如何将时间格式HH:MM:SS转换为固定的秒数?
HH:MM:SS
PS Time有时可能MM:SS仅采用格式。
MM:SS
不需要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($hours) ? $hours * 3600 + $minutes * 60 + $seconds : $minutes * 60 + $seconds;