-1

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
3ND
  • 430
  • 1
  • 6
  • 17
  • Timezone CET exists for backward compatibility reasons and shouldn't be used. See the [PHP page](http://php.net/manual/en/timezones.others.php) –  Dec 12 '14 at 02:37

1 Answers1

2

%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

John Conde
  • 217,595
  • 99
  • 455
  • 496