我想计算小时和分钟总数与另一个小时和分钟总数之差,例如:
$firsttotal = 181:33; ( 181 hours and 33 minutes)
$secondtotal = 315:00; (315 hours and 0 minutes)
所以我想要一种计算其差异的方法,如
$difference = 134:07 (134 hours and 7 minutes is the correct answer)
由于php strtotime没有转换它,而且php date函数对此也无效。
答案是(基于两个输入)133:27
,而不是134:07
。
在PHP中,您可以进行最少的数据检查。
// 181 hours and 33 minutes
$firsttotal = "181:33";
// 315 hours and 0 minutes
$secondtotal = "315:00";
// Convert to min.
// Assumes $str is a time in hhh:mm format only.
// For example, "181:33" is 181 hours and 33 min.
function toMin($str) {
$tmp = explode(":",$str);
return $tmp[1] + (60 * $tmp[0]);
}
// Return the difference in two min counts in HHH:MM format.
function diffToHHMM($min1,$min2) {
$diff = $min2 - $min1;
$h = floor($diff / 60);
$m = ($diff % 60);
return $h.":".$m;
}
$val1 = toMin($firsttotal);
$val2 = toMin($secondtotal);
echo diffToHHMM($val1, $val2);
// Result: 133:27