I am trying to write a function that will add time stamps together to get a sum of all. For example:11:30 + 12:00 + 15:35 = 39:05 I am unsure of how to accomplish this. I have included code below that I have tried but it does not give the desired result:
$TotalTime = strtotime($data['TotalSunday']) + strtotime($data['TotalMonday'])
+ strtotime($data['TotalTuesday'])
+ strtotime($data['TotalWednesday']) + strtotime($data['TotalThursday'])
+ strtotime($data['TotalFriday']) + strtotime($data['TotalSaturday']);
$data['TotalTime'] = gmdate("h:i", $TotalTime);
The problem is the following, you do want to add “times”, but in fact you are creating the sum of “dates”.
strtotimeexpects two parameters: #1 is a date, #2 is a offset (if not given, it is “now”).This is merely a hack, and I’m not sure if anyone would use it like that, but it works:
By setting the second parameter to 0, and providing the datetime parameter with an additional Z (for Zulu time, or UTC), times up to 23:59:59 are parsed correctly in seconds.
So if you really want to rely on strtotime, use it like that.