我想在hh:mm格式的两倍中得到差异。我正在使用这个函数
<?php
function timeDiff($firstTime,$lastTime) {
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);
$timeDiff=$lastTime-$firstTime;
return $timeDiff;
}
echo (timeDiff("10:00","20:00")/60)/60;
?>
问题是,如果我的小时数少于 24 小时,它可以完美运行。但我需要它工作长达 60 小时。喜欢60:00 - 02:25 should give me 57:35
和60:00 - 00:00 should give me 60:00
.我哪里做错了?
如果您只使用小时 + 分钟,那么您可以自己计算时间戳,如下所示:
function calculateSeconds($time) {
$timeParts = explode(':', $time);
return (int)$timeParts[0] * 3600 + (int)$timeParts[1] * 60;
}
然后在您的函数中使用它
function timeDiff($firstTime, $lastTime) {
return calculateSeconds($lastTime) - calculateSeconds($firstTime);
}
您可以使用
gmdate()
函数作为波纹管。
function timeDiff($firstTime,$lastTime) {
$a_split = explode(":", $firstTime);
$b_split = explode(":", $lastTime);
$a_stamp = mktime($a_split[0], $a_split[1]);
$b_stamp = mktime($b_split[0], $b_split[1]);
if($a_stamp > $b_stamp)
{
$diff = $a_stamp - $b_stamp; //69600
}else{
$diff = $b_stamp - $a_stamp; //69600
}
$min = gmdate("i", $diff);
$d_hours = gmdate("d", $diff)==1 ? 0 : gmdate("d", $diff)*12 ;
$hours = $d_hours + gmdate("H", $diff) ;
echo $hours . ':' . $min; // 56:12:12
}
timeDiff("35:05", "01:45");