monkeypaw201 Posted July 30, 2008 Share Posted July 30, 2008 How can I add 1:30 (HH:MM) and 2:01 (HH:MM) ? They are both times to reach a destination, and I want to be able to add them up.. Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/ Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Convert the original time to a timestamp and do: $minute = 60 * 60; $hour = 60 * 60 * 60; $newtime = $origtime + ($hour + ($minute * 30)); Something like that. Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/#findComment-603834 Share on other sites More sharing options...
craygo Posted July 30, 2008 Share Posted July 30, 2008 try this <?php $time1 = "1:30"; $time2 = "2:41"; $t1 = explode(":", $time1); $t2 = explode(":", $time2); echo date("G:i:s", mktime($t1[0]+$t2[0], $t1[1]+$t2[1] , 0))."<br />"; // output 4:11:00 ?> Ray Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/#findComment-603841 Share on other sites More sharing options...
DarkWater Posted July 30, 2008 Share Posted July 30, 2008 Oh woops. I misunderstood the question actually. Use craygo's code. xD Now I understand what he wanted. Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/#findComment-603845 Share on other sites More sharing options...
Barand Posted July 30, 2008 Share Posted July 30, 2008 OK for short journey times, Ray, but not for longer ones eg $time1 = "11:30"; $time2 = "13:41"; --> 1:11:00 Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/#findComment-604006 Share on other sites More sharing options...
thebadbad Posted July 30, 2008 Share Posted July 30, 2008 This should work: <?php $time1 = '11:30'; $time2 = '13:41'; $t1 = explode(':', $time1); $t2 = explode(':', $time2); $hours = $t1[0] + $t2[0]; $minutes = $t1[1] + $t2[1]; while($minutes > 59) { $hours++; $minutes = $minutes - 60; } echo "$hours hrs, $minutes mins"; ?> Edit: Also for times like 26:99 and 432:123. For simple HH:MM (with max 23:59), the while loop can be changed to an if statement (I just realized ). Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/#findComment-604023 Share on other sites More sharing options...
Barand Posted July 30, 2008 Share Posted July 30, 2008 or, slightly shorter, $time1 = '11:30'; $time2 = '13:41'; list($h1,$m1) = explode (':', $time1); list($h2,$m2) = explode (':', $time2); $hours = $h1 + $h2 + floor(($m1+$m2)/60); $minutes = ($m1 + $m2) % 60; echo "$hours:$minutes"; Link to comment https://forums.phpfreaks.com/topic/117390-adding-130-and-201/#findComment-604032 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.