$time_taken = 286;
$time_taken = $time_taken / 60; //Converting to minutes
echo $time_taken;
结果: 4.7666666666667
但是我需要:(期望:)
结果: 5.17 (预期)
我尝试过:round($time_taken,2);
但是它给出了结果:
结果: 4.77
答案 0 :(得分:2)
您正在读取错误的结果。但是不用担心。与时间打交道使大多数开发人员一次又一次地疯狂。就像通行仪式。
您得到4.76 minutes
,与4 minutes and 76 seconds
相同。
它是4 full minutes and 0.76 of a minute
。
分解:
4 minutes = 240 sec
286 - 240 = 46
所以结果应该是4分46秒。
要进行计算,您可以执行以下操作:
$total = 286;
// Floor the minutes so we only get full minutes
$mins = floor($total / 60);
// Calculate how many secs are left
$secs = $total % 60; // Thanks @RiggsFolly for the tip
echo "$mins minutes and $secs seconds";