如何将
HH:MM:SS
格式的时间转换为单位秒数?
P.S. 时间有时可能仅采用格式
MM:SS
。
不需要
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;
如果您不想使用正则表达式:
$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;
我认为最简单的方法是使用
strtotime()
函数:
$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;
date_parse()
也可用于解析日期和时间:
$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
如果您使用
MM:SS
或 strtotime()
解析格式 date_parse()
,它将失败(date_parse()
用于 strtotime()
和 DateTime
),因为当您输入像 xx:yy
这样的格式时,解析器会假设它是 HH:MM
而不是MM:SS
。我建议检查格式,如果您只有 00:
,请在前面加上 MM:SS
。
strtotime()
date_parse()
如果您的时间超过 24 小时,那么您可以使用下一个功能(它将适用于
MM:SS
和 HH:MM:SS
格式):
function TimeToSec($time) {
$sec = 0;
foreach (array_reverse(explode(':', $time)) as $k => $v) $sec += pow(60, $k) * $v;
return $sec;
}
$time = 00:06:00;
$timeInSeconds = strtotime($time) - strtotime('TODAY');
您可以使用
strtotime
函数返回从 today 00:00:00
开始的秒数。
$seconds= strtotime($time) - strtotime('00:00:00');
伪代码:
split it by colon
seconds = 3600 * HH + 60 * MM + SS
试试这个:
$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;
简单
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
在 Windows 32 位 PHP 版本:7.2.31 上,我在此处发布的所有版本上都会出现一些错误。 如果时间为 00:00:00 或 00:00:00,则返回零 00 并用作“”空字符串,并且使用空字符串的计算返回错误“A Non WELLNUMERIC blabla。
这也适用于 24 小时以上:
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");
涵盖 HH:MM:SS、MM:SS 和 SS 情况。
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
`
<?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];