How to turn time in format HH:MM:SS
into a flat seconds number?
P.S. Time could be sometimes in format MM:SS
only.
How to turn time in format HH:MM:SS
into a flat seconds number?
P.S. Time could be sometimes in format MM:SS
only.
No need to explode
anything:
$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 don't want to use regular expressions:
$str_time = "2:50";
sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
Should be $time_seconds = isset($hours) ? $hours * 3600 + $minutes * 60 + $seconds : $minutes * 60 + $seconds;
–
Paquito [\d]
is more simply expressed as \d
. –
Supertax I think the easiest method would be to use 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;
Function date_parse()
can also be used for parsing date and time:
$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
If you will parse format MM:SS
with strtotime()
or date_parse()
it will fail (date_parse()
is used in strtotime()
and DateTime
), because when you input format like xx:yy
parser assumes it is HH:MM
and not MM:SS
. I would suggest checking format, and prepend 00:
if you only have MM:SS
.
demo strtotime()
demo date_parse()
If you have hours more than 24, then you can use next function (it will work for MM:SS
and HH:MM:SS
format):
function TimeToSec($time) {
$sec = 0;
foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
return $sec;
}
strtotime()
does not allow the hours go beyond 24 - using 25 or higher will return "false". –
Pewter array_reverse
and explode on :
then causing one more trip through the foreach
and another pow
(so that both hh:mm AND hh:mm:ss work without modification of the function) is a stroke of genius. Code par excellence. I can hardly imagine a more efficient example. Thanks! –
Elanorelapid .
like this 00:20:55.60000
? I tried to post a question, but there seems a problem. –
Judaic $seconds = strtotime("1970-01-01 $time UTC");
should be the shortest answer and the accepted one. The use of 1970-01-01
is genial! –
Beecher $time = 00:06:00;
$timeInSeconds = strtotime($time) - strtotime('TODAY');
You can use the strtotime
function to return the number of seconds from today 00:00:00
.
$seconds= strtotime($time) - strtotime('00:00:00');
In pseudocode:
split it by colon
seconds = 3600 * HH + 60 * MM + SS
Try this:
$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;
Simple
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;
}
function time2sec($time) {
$durations = array_reverse(explode(':', $item->duration));
$second = array_shift($durations);
foreach ($durations as $duration) {
$second += (60 * $duration);
}
return $second;
}
echo time2sec('4:52'); // 292
echo time2sec('2:01:42'); // 7302
$item->duration
, which should just be replaced by $time
. And it should probably do some casting. –
Grilse On Windows 32 bit PHP version: 7.2.31 i get some error on all versions posted here. If the time was 00:00:00 or 00:00:00 the zeros 00 were returned and used as "" empty string, and calculation with empty string returns error "A Non WELLNUMERIC blabla.
This Works also with more then 24hours:
function TimeToSec(string $time) {
$timearray = explode(":",$time);
$hours = (int)$timearray[0];
$minutes = (int)$timearray[1];
$seconds = (int)$timearray[2];;
//echo "Hours: " . $hours ."<br>";
//echo "minutes: " . $minutes ."<br>";
//echo "seconds: " . $seconds ."<br>";
$value = ($hours * 3600) + ($minutes * 60) + $seconds;
return $value;
}
echo TimeToSec("25:00:30");
To cover HH:MM:SS, MM:SS, and SS cases.
function getSeconds(string $time) {
$match = sscanf($time, "%d:%d:%d", $t1, $t2, $t3);
return match ($match) {
1 => $t1,
2 => $t1 * 60 + $t2,
3 => $t1 * 3600 + $t2 * 60 + $t3,
default => 0
};
}
echo getSeconds("02:50"); // 170
echo getSeconds("1:02:50"); // 3770
echo getSeconds("50"); // 50
`
Will work with any format ("HH:MM:SS", "MM:SS" or "SS")
function timeToSeconds(string $time): int {
return array_reduce(explode(':', $time), fn ($x, $i) => $x * 60 + $i, 0);
}
<?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];
© 2022 - 2024 — McMap. All rights reserved.