将数字时间转换为秒
Convert Digit Time to seconds
如何将数字时间转换成秒?
我需要比较时间,所以我认为将数字时间转换为秒并稍后比较秒会更容易。
例如:
00:00:33
00:01:33
02:01:33
您想使用 strtotime()
。这会将 "digit time" 转换为 Unix 时间。
$time_in_seconds = strtotime('00:00:33');
如果您要从该格式中寻找精确的秒数,您可以编写自定义解析器。
像这样应该可以解决问题:
echo hhmmss2seconds("18:24:35");
function hhmmss2seconds($time) {
$digits = explode(":", $time);
$seconds = 0;
$seconds = $seconds + intval($digits[0]) * 3600; // hours
$seconds = $seconds + intval($digits[1]) * 60; // minutes
$seconds = $seconds + intval($digits[2]); // seconds
return $seconds;
}
试试这个:
echo strtotime("1970-01-01 00:00:11 UTC");
尝试一下它的工作方式
<?php
function time_to_seconds($time) {
list($h, $m, $s) = explode(':', $time);
return ($h * 3600) + ($m * 60) + $s;
}
echo time_to_seconds("00:00:33");
echo time_to_seconds("00:01:33");
echo time_to_seconds("02:01:33");
?>
谢谢..
如何将数字时间转换成秒?
我需要比较时间,所以我认为将数字时间转换为秒并稍后比较秒会更容易。
例如:
00:00:33
00:01:33
02:01:33
您想使用 strtotime()
。这会将 "digit time" 转换为 Unix 时间。
$time_in_seconds = strtotime('00:00:33');
如果您要从该格式中寻找精确的秒数,您可以编写自定义解析器。
像这样应该可以解决问题:
echo hhmmss2seconds("18:24:35");
function hhmmss2seconds($time) {
$digits = explode(":", $time);
$seconds = 0;
$seconds = $seconds + intval($digits[0]) * 3600; // hours
$seconds = $seconds + intval($digits[1]) * 60; // minutes
$seconds = $seconds + intval($digits[2]); // seconds
return $seconds;
}
试试这个:
echo strtotime("1970-01-01 00:00:11 UTC");
尝试一下它的工作方式
<?php
function time_to_seconds($time) {
list($h, $m, $s) = explode(':', $time);
return ($h * 3600) + ($m * 60) + $s;
}
echo time_to_seconds("00:00:33");
echo time_to_seconds("00:01:33");
echo time_to_seconds("02:01:33");
?>
谢谢..