获取php中两个日期之间的天数

问题描述:

我试图获取两个日期之间的天数,但返回的结果不正确。这是我的尝试;获取php中两个日期之间的天数

$t_time = get_the_time(__('Y/m/d g:i:s A', 'woocommerce'), $post); //date of post 
    $start_date = new DateTime(); //current date 
    $since_start = $start_date->diff(new DateTime($t_time)); //difference 
    echo $since_start->d; //number of days 

我哪里出错了?它适用于前几个帖子,但是当月份发生变化时它会重新启动。

+2

[** ** RTM(http://php.net/manual/en/class.dateinterval.php)'$ since_start-> D'!= ='$ since_start-> days' Rizier123

试试这个。

<?php 

$currentTime = time(); 

$startTime = strtotime("YYYY/MM/DD"); 

$diff = $currentTime - $startTime; 

echo floor($diff/(60*60*24)); // Number of days between $currentTime and $startTime 

?> 

试试这个

<?php 

    $now = time(); // Current time 
    $your_date = strtotime("2013-12-01"); // This will parses an English textual datetime into a Unix timestamp 
    $datediff = abs($now - $your_date);// Gives absolute Value 
    echo floor($datediff/(60*60*24)); //Returns the lowest value by rounding down value 

?> 
+0

你可以添加一些解释吗? –

+0

@AdrianCidAlmaguer解释已添加! – Yogus

+0

谢谢,现在对用户更好;-) –