Page 1 of 1
adding time
Posted: Thu Apr 20, 2006 8:11 am
by gurjit
Hi,
I have the time taken to do a task stored like this:
01:15:20 // this is 1 hour 15 minutes and 20 seconds
13:30:30 // this is 13 hours 30 minutes and 30 seconds
how can i add these two times up and calculate the total time taken, to give the answer
14:45:50 // 14 hours 45 minutes and 50 seconds
????????????????
Posted: Thu Apr 20, 2006 11:10 am
by feyd
convert them into a single number (say in seconds), then add the two and break it back apart into the "nice" version.
Posted: Fri Apr 21, 2006 2:43 am
by gurjit
i added them up in variables
$hours
$minutes
$seconds
and used the below code to add and manipulate the data but it goes over 60 minutes... what am i doing wrong:
for example it says:
21 Hours 66 Mins 26 Seconds
Code: Select all
<?php echo ($hours + (int)($minutes/60)). ' Hours '.((int)($minutes%60) + (int)($seconds/60)).' Mins '.(int)($seconds%60).' Seconds '; ?>
Posted: Fri Apr 21, 2006 3:06 am
by RobertGonzalez
How did you get your $hours, $minutes and $seconds?
Posted: Fri Apr 21, 2006 4:07 am
by gurjit
I have the total time taken stored in the database like 01:10:12, 1 hour, 10 minutes and 12 seconds, so i just keep adding the values to the variables like this:
Code: Select all
<?php
$hours = $hours + date("G",strtotime($tt_total_time_taken));
$minutes = $minutes + date("i",strtotime($tt_total_time_taken));
$seconds = $seconds + date("s",strtotime($tt_total_time_taken));
?>
I found a class which does exactly what i needed because i total the hours, minutes and seconds - i took you advice and worked out the seconds using the following class:
http://aidan.dotgeek.org/repos/?file=Duration.php
Thanks for the tip..... too early for me to think and its friday