Convert time in HH: MM: SS format only for seconds?

How to turn time in HH:MM:SS format into the number of "flat seconds"?

PS Time can sometimes be only in the format MM:SS .

+59
php time
Jan 29 2018-11-11T00:
source share
11 answers

Don't need anything explode

 $str_time = "23:12:95"; $str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time); sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds); $time_seconds = $hours * 3600 + $minutes * 60 + $seconds; 

And if you do not want to use regular expressions:

 $str_time = "2:50"; sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds); $time_seconds = isset($hours) ? $hours * 3600 + $minutes * 60 + $seconds : $minutes * 60 + $seconds; 
+106
Jan 29 '11 at 0:10
source share

I think the simplest method will use the strtotime() function:

 $time = '21:30:10'; $seconds = strtotime("1970-01-01 $time UTC"); echo $seconds; // same with objects (for php5.3+) $time = '21:30:10'; $dt = new DateTime("1970-01-01 $time", new DateTimeZone('UTC')); $seconds = (int)$dt->getTimestamp(); echo $seconds; 

demonstration




The date_parse() function can also be used to parse the date and time:

 $time = '21:30:10'; $parsed = date_parse($time); $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second']; 

demonstration




If you strtotime() MM:SS format using strtotime() or date_parse() , it will fail ( date_parse() used in strtotime() and DateTime ) because when you enter a format like xx:yy , the parser assumes HH:MM , not MM:SS . I would suggest checking the format and adding 00: if you only have MM:SS .

demo strtotime() demo date_parse()




If you have a clock more than 24, you can use the following function (it will work in the format MM:SS and HH:MM:SS ):

 function TimeToSec($time) { $sec = 0; foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v; return $sec; } 

demonstration

+86
Jan 02 '14 at 0:24
source share

In pseudo code:

 split it by colon seconds = 3600 * HH + 60 * MM + SS 
+5
Jan 29 2018-11-11T00:
source share

Try the following:

 $time = "21:30:10"; $timeArr = array_reverse(explode(":", $time)); $seconds = 0; foreach ($timeArr as $key => $value) { if ($key > 2) break; $seconds += pow(60, $key) * $value; } echo $seconds; 
+5
Jan 29 2018-11-11T00:
source share
  $time = 00:06:00; $timeInSeconds = strtotime($time) - strtotime('TODAY'); 
+4
Jan 09 '17 at 10:28
source share

You can use the strtotime function to return the number of seconds from today 00:00:00 .

 $seconds= strtotime($time) - strtotime('00:00:00'); 
+2
Jan 23 '18 at 12:03
source share

Plain

 function timeToSeconds($time) { $timeExploded = explode(':', $time); if (isset($timeExploded[2])) { return $timeExploded[0] * 3600 + $timeExploded[1] * 60 + $timeExploded[2]; } return $timeExploded[0] * 3600 + $timeExploded[1] * 60; } 
+1
09 Oct '15 at 5:58
source share
 <?php $time = '21:32:32'; $seconds = 0; $parts = explode(':', $time); if (count($parts) > 2) { $seconds += $parts[0] * 3600; } $seconds += $parts[1] * 60; $seconds += $parts[2]; 
0
Jan 29 2018-11-11T00:
source share
 $time = '01:21:34'; //time in h:s:m $timeToSeconds = (new DateTime('1970/01/01 ' . $time))->getTimestamp(); 
0
Dec 21 '18 at 13:31
source share
 $time="12:10:05"; //your time echo strtotime("0000-00-00 $time")-strtotime("0000-00-00 00:00:00"); 
-one
Dec 06
source share
 // HH:MM:SS or MM:SS echo substr($time , "-2"); // returns last 2 chars : SS 
-3
Jan 29 2018-11-11T00:
source share



All Articles