Function to convert time to number of seconds

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

+6
php mysql
source share
7 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 
+11
source share
 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 ()

+8
source share

Use the following program to convert time in seconds to php.

 <?php function time_to_sec($time) { $hours = substr($time, 0, -6); $minutes = substr($time, -5, 2); $seconds = substr($time, -2); return $hours * 3600 + $minutes * 60 + $seconds; } $result=time_to_sec('12:25:59'); echo $result; ?> 
+1
source share

$ current_time_in_seconds = "01: 21: 44,24";

  list($hours, $minutes, $seconds) = explode(":", $current_time_in_seconds); $current_time_in_seconds=$hours*3600+$minutes*60+$seconds; 

will receive from

01: 21: 44.24

to

4904.24

+1
source share

so if mysql without fractions is not suitable for the solution - here is another mine

 $time = '1:23:33.03'; $parts = explode(':', $time); $seconds = 0; foreach ($parts as $i => $val) { $seconds += $val * pow(60, 2 - $i); } echo $seconds; 
0
source share

Strtotime is what you need. You get a Unix timestamp, which in this case will be the number of seconds.

-one
source share

I'm a bit confused, but I think you mean it. 1 hour, 23 minutes and 23.03 seconds.

it's not hard. I made this PHP function that does this. My php server does not want to start, so I could not test it, but I think it should work.

  function ConvertTimeToSeconds ($ T)
 {
     $ exp = explode (":", $ T);
     $ c = count ($ exp);
     $ r = 0;
     if ($ c == 2)
     {
         $ r = (((int) $ exp [0]) * 60) + ((int) $ exp [1]);
     } else {
         $ r = (((int) $ exp [0]) * 3600) + (((int) $ exp [1]) * 60) + ((int) $ exp [2]);
     } 

     return $ r;
 }

 ConvertTimeToSeconds ("1: 23: 33.03");
-one
source share

All Articles