Adding times together???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gloveny2
Forum Newbie
Posts: 9
Joined: Wed Mar 31, 2004 10:00 am

Adding times together???

Post 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
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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");
?>
:)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post 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 ? :D

well we don't live in an infinite world :lol:
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

lol, its just a idea....a wrong one, but its still a idea :P
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

qads wrote:lol, its just a idea....a wrong one, but its still a idea :P
:) yepp, but i agree with twiglet and pickle. i would prefer timestamp thing, too.
gloveny2
Forum Newbie
Posts: 9
Joined: Wed Mar 31, 2004 10:00 am

Post 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++)
&#123;
	$split = explode(":", $times&#1111;$x]); 
	$hou += $split&#1111;0];
	$min += $split&#1111;1];
	$sec += $split&#1111;2];
&#125;
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.
Post Reply