简体   繁体   中英

php DateTime acting weird

So i have this code to determine difference between present time and datetime field value from database.

This code gives weird output (samples below) for dates that are not in same year.

$deadline      = new DateTime($someDeadline, new DateTimeZone('CET'));
$time_now      = new DateTime('NOW', new DateTimeZone('CET'));
$overdue_until = $time_now->diff($deadline);
echo $overdue_until->format('%R %d day/s, %h hours, %i minutes');

Example results:

Monday 9th of February 2015 05:28:23 PM gets the difference + 28 day/s, 17 hours, 6 minutes from current time.

Wednesday 14th of January 2015 11:28:43 AM + 2 day/s, 10 hours, 34 minutes

%d gets you up to 31 days. After that it resets to zero and months are incremented. If you want the total number if days you need to use %a

echo $overdue_until->format('%R %a day/s, %h hours, %i minutes');

Demo 1 Demo 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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