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
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho