我需要将小时:分钟:秒转换为秒
您可以尝试使用
strtotime('00:02:05') - strtotime('today'); //125
演示
您可以在PHP:中使用爆炸函数
function seconds($time){
$time = explode(':', $time);
return ($time[0]*3600) + ($time[1]*60) + $time[2];
}
示例:http://codepad.org/gAjZGtq3
strtotime
可能会有所帮助。文档
或者,
<?php
$parts = explode(":", $my_str_time); //if you know its safe
$answer = ($parts[0] * 60 * 60 + $parts[1] * 60 + $parts[2]) . "s";
DateTime
类中可能存在某些内容。但没有单一的方法。此外,您还需要将格式转换为日期-时间对象,然后再转换回来。
编写自己的很琐碎
function toSeconds($hours, $minutes, $seconds) {
return ($hours * 3600) + ($minutes * 60) + $seconds;
}