Adding times together???
Moderator: General Moderators
Adding times together???
Hi everyone, I have a table with all sorts of data, but one column has a time value. I want to be able to add these times up using PHP.
The format of the time is this> 00:00:00 or hh:mm:ss .
So I could have 10 of these dates to add. Can anyone tell me if this is possible?.
Thanks alot.
Graham
The format of the time is this> 00:00:00 or hh:mm:ss .
So I could have 10 of these dates to add. Can anyone tell me if this is possible?.
Thanks alot.
Graham
Code: Select all
<?php
function add_time($time1, $time2)
{
$split1 = explode(":", $time1);
$split2 = explode(":", $time2);
$hours = $split1[0] + $split2[0];
$mins = = $split1[1] + $split2[1];
$secs = $split1[2] + $split2[2];
return $hours.':'.$mins.':'.$secs;
}
/////////////
echo add_time("10:22:15", "1:12:05");
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- mudkicker
- Forum Contributor
- Posts: 479
- Joined: Wed Jul 09, 2003 6:11 pm
- Location: Istanbul, TR
- Contact:
I think you should check the $hours,$mins,$secs > 60, huh ?qads wrote:Code: Select all
<?php function add_time($time1, $time2) { $split1 = explode(":", $time1); $split2 = explode(":", $time2); $hours = $split1[0] + $split2[0]; $mins = = $split1[1] + $split2[1]; $secs = $split1[2] + $split2[2]; return $hours.':'.$mins.':'.$secs; } ///////////// echo add_time("10:22:15", "1:12:05"); ?>
well we don't live in an infinite world
Like ~twigletmac said, UNIX timestamps are by far the easiest for this.
Say you have two times, 1:15:00 and 12:52:00. The easiest way to do this is as follows:
Keep in mind that this is assuming 24 hour clocks and doesn't account for day wrap over (ie: 23:00 + 2:00 would give 1:00, not 1:00 plus a day)
Say you have two times, 1:15:00 and 12:52:00. The easiest way to do this is as follows:
Code: Select all
$first_time = "1:15:00";
$second_time = "12:52:00";
$first_exploded = explode(":",$first_time);
$second_exploded = explode(":",$second_time);
$first_stamp = mktime($first_exploded[0],$first_exploded[1],$first_exploded[2],1,1,1980);
$second_stamp = mktime($second_exploded[0],$second_exploded[1],$second_exploded[2],1,1,1980);
$time_added = $first_stamp + $second_stamp;
$sum_time = date("H:i:s",$time_added);Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Cheers everyone, someone at work suggested using the MOD(%) thing to do it, and I came up with this...
Thanks all who posted.
Code: Select all
<?php
$times = array("00:00:00","00:02:00","00:03:12","00:12:45","00:32:10","12:12:12");
$hou = 0;
$min = 0;
$sec = 0;
$length = sizeof($times);
for($x=0;$x<$length;$x++)
{
$split = explode(":", $timesї$x]);
$hou += $splitї0];
$min += $splitї1];
$sec += $splitї2];
}
echo "h=$hou m=$min s=$sec<BR>";
$seconds = $sec%60;
$minutes = $sec/60;
$minutes =(integer)$minutes;//turn into an int to get rid of the deciaml place.
$minutes += $min;
$hours = $minutes/60;
$minutes = $minutes%60;
$hours =(integer)$hours;//turn into an int to get rid of the deciaml place.
$hours += $hou%24;
echo$hours.":".$minutes.":".$seconds;
?>