Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.