php - Difference between 2 datetimes in minutes? -
i got following code:
$now = new datetime(); $then = new datetime($accountexists['sub_limit']); $interval = $then->diff($now); $hours = $interval->format('%h'); $minutes = $interval->format('%i'); echo 'diff. in minutes is: '.($hours * 60 + $minutes);
which returns difference between 2 datetimes in minutes. if then
2015-05-31 19:15:31
, now
2015-05-31 19:20:31
returns 5 minutes. day changes, if then
changes 2015-05-30 19:15:31
still returns 5 minutes when should 1445 minutes. point out error?
because months, years can have arbitrary number of minutes, might best want convert dates timestamps (seconds since epoch) have divide 60. fortunately, it's easy so:
$now = new datetime('2015-05-31 19:20:31'); $then = new datetime('2015-05-30 19:15:31'); $seconds = abs($now->format('u') - $then->format('u')); $minutes = floor($seconds / 60); print $minutes;
Comments
Post a Comment