2010-06-12 113 views
3

這裏是我的代碼日期時間diff不能工作

function check($dt) { 
    $date = date("Y-m-d"); 
    $start = new DateTime($date); 
    $end = new DateTime($dt); 
    $diff = $start->diff($end); 

    return $diff->format('%d days'); 
    } 

print check('2009-12-14'); 

,打印29天

我在哪裏錯了?

回答

6

它在manual解釋說:

<?php 

$january = new DateTime('2010-01-01'); 
$february = new DateTime('2010-02-01'); 
$interval = $february->diff($january); 

// %a will output the total number of days. 
echo $interval->format('%a total days')."\n"; 

// While %d will only output the number of days not already covered by the 
// month. 
echo $interval->format('%m month, %d days'); 

?> 

你想:

function check($dt) { 
    $date = date("Y-m-d"); 
    $start = new DateTime($date); 
    $end = new DateTime($dt); 
    $diff = $start->diff($end); 

    return $diff->format('%a days'); 
} 

print check('2009-12-14'); 

180 days

+0

哦是的,我錯過了手冊中的那部分 – 2010-06-12 14:41:05