I'm trying to subtract the time difference between the current datetime, and a time stated in my database. The datetimes current format is yyyy:mm:dd hh:mm:ss, however for this specific case i just want to subtract the time and not the date so i only want hh:mm:ss to be calculated and then stored into a different variable i can use and format how i want. Is it possible to take a full datetime, break it apart and do a diff on it? I think this is kind of confusing so ask if you need clarification. Here's what i've tried thus far:
<?php
//The time in the database
$classTime = DateTime::createFromFormat('Y-m-d H:i:s', '0000-00-00 18:30:00');
$timein = new DateTime("now", new DateTimeZone('America/Detroit'));
$timein->getTimestamp();
$timeout = $classTime;
$totaltime = $timeout->diff($timein);
$totaltime = $totaltime->format('Y-m-d H:i:s');
$totaltime = date('0000:00:00 H:i:s', strtotime($totaltime));
//I create a new date because i'm storing this time into the database, which can't be done with a datetime.
//FORMAT TIMES
$timein = $timein->format('Y-m-d H:i:s');
$timeout = $timeout->format('Y-m-d H:i:s');
echo "Time in " . $timein . " Time Out " . $timeout . " Total Time " . $totaltime;
?>
This current output returns:
Time in 2013-04-02 14:05:32 Time Out -0001-11-30 18:30:00 Total Time 0000:00:00 00:00:00
But i want it to return something like:
Time in 2013-04-02 14:05:32 Time Out -0000-00-00 18:30:00 Total Time 0000:00:00 03:30:00
DateTimecan't calculate it correctly. Time is based of time stamps with 0 being Jan 1 1970 UTC (if I remember correctly), so anything left after a calculation only using the hours/min/sec of a stamp will give the number of seconds difference, so from there you'd have to calculate that in to hours:min:sec by hand.