简体   繁体   English

datetime diff不起作用

[英]datetime diff doesn't work

here is my code 这是我的代码

    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');

that prints 29 days 打印29天

where am i wrong ? 我哪里错了?

It's explained in the 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');

?>

You want: 你要:

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');

gives 180 days . 给了180 days

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM