如何计算小时和分钟的差异?在我的下面的代码中,
echo $d = ((strtotime("18:00:00") - strtotime("17:30:00"))/60)/60;
Actual Result: 0.5
Expected Result: 0.30
echo $d = ((strtotime("19:00:00") - strtotime("17:30:00"))/60)/60;
Actual Result: 1.5
Expected Result: 1.30
怎么做呢?
答案 0 :(得分:1)
使用:
echo $d = round((strtotime("18:00:00") - strtotime("17:30:00"))/60, 1);
由于您需要转换为分钟,因此除以60 使用round()限制为一个小数点。
修改(问题更新后回答)
您可以使用gmdate功能:
$d = strtotime("19:00:00") - strtotime("17:30:00");
echo gmdate("G.i", $d);
使用G
代替H
,因为您需要小时而不会导致零。
结果:
1.30
答案 1 :(得分:1)
只需将差值除以60.因为你只需要Min,如果你想有效地格式化它,那么需要更多的形成。但这仅适用于最小差异。
$d = strtotime("18:00:00") - strtotime("17:30:00"); echo ($d/60)." minutes";
结果: 30分钟
所以你希望得到像1.30
这样的差异。试试这个:
$d = strtotime("18:00:00") - strtotime("16:40:00");
$d = $d/60;
if($d > 59){
$m = $d%60;
$d = intval($d/60);
echo $d.".".$m;
}else
echo $d. " Miniutes";