如何将格式化 HH:MM:SS的时间转换为平面秒数?
HH:MM:SS
时间有时只能是 MM:SS格式。
MM:SS
在伪代码中:
split it by colon seconds = 3600 * HH + 60 * MM + SS
<?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];
不需要 explode任何东西:
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;
试试这个:
$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;
我认为 最简单的方法将使用 strtotime()函数:
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()也可用于解析日期和时间:
date_parse()
$time = '21:30:10'; $parsed = date_parse($time); $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
如果你用 strtotime()或者 date_parse()解析 MM:SS格式,那么它将会失败(date_parse()在 strtotime()和 DateTime中使用) ,因为当你输入像 xx:yy解析器这样的格式时,假设它是 HH:MM而不是 MM:SS。我会建议检查格式,并预先 00:如果你只有 MM:SS。
DateTime
xx:yy
HH:MM
00:
演示 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; }
很简单
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; }
$time = 00:06:00; $timeInSeconds = strtotime($time) - strtotime('TODAY');
可以使用 strtotime函数从 today 00:00:00返回秒数。
strtotime
today 00:00:00
$seconds= strtotime($time) - strtotime('00:00:00');
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,则返回0,并将其用作“”空字符串,而使用空字符串进行计算将返回错误“ 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");