猿问

PHP中的小数点后舍入时间

$time_taken = 286;


$time_taken = $time_taken / 60;  //Converting to minutes


echo $time_taken;

结果:4.7666666666667


但我需要:(期望:)


结果:5.17(预期)


我试过了: round($time_taken,2);


但后来它给出了结果:


结果:4.77


慕桂英546537
浏览 162回答 1
1回答

暮色呼如

你读错了结果。不过别担心。与时间打交道曾多次让大多数开发人员发疯。这就像一个成人礼。你得到4.76 minutes,这不一样4 minutes and 76 seconds。它是4 full minutes and 0.76 of a minute。分解一下:4 minutes = 240 sec286 - 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 tipecho "$mins minutes and $secs seconds";
随时随地看视频慕课网APP
我要回答