Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
addition of time

Posted by bschultz (bschultz), 27 October 2003
Can someone point me in the right direction on how to add time in PHP.

For example:

The format would be like this
1:02.8 (1 minute, 2 seconds, 8 1/10ths)

I need to add that to get 2:08.0 (2 minutes, 8 seconds)

Then, I'll need to divide that number by the number of races run for the average time per race (which in this case would be (1:04.0)

Is this possible in PHP?



Posted by admin (Graham Ellis), 27 October 2003
Of course - anything's possible in PHP  

[Sorry - been a long day's drive and it has warped my sense of humour]

The best way to deal with time arithemetic (in any language) is to convert to seconds, do the sums, and convert back again.  I would suggest a regular expression match to extract the minutes and seconds, multipy the minutes by 60 and add the seconds, do the maths, and use the / and % operators to convert back, followed by sprintf.

Would you like me to code an example?  Too tired to do so tonight (It's 11 pm in Dublin where I am) but I could do so tomorrow evening ....

Posted by bschultz (bschultz), 28 October 2003

I can strip it down to seconds...but would need some help getting it back into a time format.

Any help would be greatly appreciated.


(hope you get a good night's sleep!

Posted by admin (Graham Ellis), 28 October 2003
Example of formatting a time in seconds into minutes and seconds, as per your format:

<input name=seconds>
if ($seconds > 0) {
       $mins = floor ($seconds / 60);
       $secs = $seconds % 60;
       printf ("%.2f seconds should display as %d:%02.1f<br>",
               $seconds, $mins, $secs);

Available for test at

Got a good night's sleep, yes, thanks ... much recovered now (24 hours later).

Posted by bschultz (bschultz), 29 October 2003
thanks, Graham.  Pretty easy to understand now that I see it in front of my face.  I appreciate it!

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho