Page 1 of 1
Adding times together???
Posted: Wed Mar 31, 2004 10:00 am
by gloveny2
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
Posted: Wed Mar 31, 2004 10:34 am
by qads
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");
?>

Posted: Wed Mar 31, 2004 11:50 am
by twigletmac
The best thing would be to get the times converted to UNIX timestamps, if you're getting the data from a database you could possibly use one of the db's functions to do it in a SQL query or you could [php_man]explode[/php_man]() the times and then use [php_man]mktime[/php_man]().
Mac
Posted: Wed Mar 31, 2004 12:11 pm
by mudkicker
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");
?>

I think you should check the $hours,$mins,$secs > 60, huh ?
well we don't live in an infinite world

Posted: Wed Mar 31, 2004 2:58 pm
by qads
lol, its just a idea....a wrong one, but its still a idea

Posted: Wed Mar 31, 2004 2:58 pm
by pickle
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:
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);
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)
Posted: Wed Mar 31, 2004 3:00 pm
by mudkicker
qads wrote:lol, its just a idea....a wrong one, but its still a idea


yepp, but i agree with twiglet and pickle. i would prefer timestamp thing, too.
Posted: Thu Apr 01, 2004 3:21 am
by gloveny2
Cheers everyone, someone at work suggested using the MOD(%) thing to do it, and I came up with this...
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;
?>
Thanks all who posted.