Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datetime diff doesn't work

Tags:

php

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 ?

like image 875
Utku Dalmaz Avatar asked Oct 15 '25 14:10

Utku Dalmaz


1 Answers

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.

like image 97
Artefacto Avatar answered Oct 18 '25 11:10

Artefacto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!