Jump to content

Adding 1:30 And 2:01


monkeypaw201

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.