2015-07-03 74 views
-2

我試圖獲取兩個日期之間的天數,但返回的結果不正確。這是我的嘗試;獲取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

回答

0

試試這個。

<?php 

$currentTime = time(); 

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

$diff = $currentTime - $startTime; 

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

?> 
1

試試這個

<?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

謝謝,現在對用戶更好;-) –

相關問題