adding time

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
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

adding time

Post 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

????????????????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

convert them into a single number (say in seconds), then add the two and break it back apart into the "nice" version.
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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 '; ?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

How did you get your $hours, $minutes and $seconds?
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post 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
Post Reply