开发者

datetime diff doesn't work

开发者 https://www.devze.com 2023-01-03 18:40 出处:网络
here is my code function 开发者_如何学运维check($dt) { $date = date(\"Y-m-d\"); $start = new DateTime($date);

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号