I’m trying to get the difference between two datetimes and return it as a datetime
. I’ve found examples using diff
but I can’t seem to get it right.
$timein = date("Y-m-d H:i:s"); $timeout = date("Y-m-d 20:00:00"); $totaltime = $timein->diff($timeout);
However $totaltime
logs 0000-00-00 00:00:00
to my DB. Is this because I’m not formatting my totaltime variable?
Advertisement
Answer
I’m not sure what format you’re looking for in your difference but here’s how to do it using DateTime
$datetime1 = new DateTime(); $datetime2 = new DateTime('2011-01-03 17:13:00'); $interval = $datetime1->diff($datetime2); $elapsed = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds'); echo $elapsed;