[英]datetime diff doesn't work
這是我的代碼
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天
我哪里錯了?
它在手冊中解釋:
<?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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.