Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function for converting time to number of seconds

Tags:

php

mysql

On our site, we have a lot of swimming times that we would like to convert to seconds. i.e. 1:23:33.03 or 58:22.43. Is there a PHP function that can do this? A MySQL function?

like image 361
Brian Avatar asked Mar 15 '10 23:03

Brian


People also ask

What is the formula of time in seconds?

To convert time to seconds, multiply the time time by 86400, which is the number of seconds in a day (24*60*60 ).

How do you convert HH mm s to seconds in Excel?

To convert hh:mm:ss time format to minutes: =((HOUR(A2)*60)+MINUTE(A2)+(SECOND(A2)/60)); To convert hh:mm:ss time format to seconds: =HOUR(A2)*3600 + MINUTE(A2)*60 + SECOND(A2).

Can you convert hours to seconds using formula?

How do I convert 1 hour in seconds? Multiply the hours by 60 to convert it to minutes, i.e., 1 hr × 60 = 60 minutes . Multiply the minutes by 60 to obtain the number of seconds, i.e., 60 minutes × 60 = 3600 seconds .


2 Answers

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_time-to-sec

mysql> SELECT TIME_TO_SEC('22:23:00');
    -> 80580
mysql> SELECT TIME_TO_SEC('00:39:38');
    -> 2378
like image 188
zerkms Avatar answered Sep 25 '22 11:09

zerkms


function time2seconds($time='00:00:00')
{
    list($hours, $mins, $secs) = explode(':', $time);
    return ($hours * 3600 ) + ($mins * 60 ) + $secs;
}

From here.

MySQL also has TIME_TO_SEC()

like image 35
jasonbar Avatar answered Sep 24 '22 11:09

jasonbar