小数点后 PHP 的舍入时间

Rounding time in PHP after decimal

$time_taken = 286;

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

echo $time_taken;

Result: 4.7666666666667

但我需要:(期望:)

Result: 5.17 (Expected)

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

但随后给出了结果:

Result: 4.77

你读错了结果。不过别担心。与时间打交道曾一度让大多数开发者发疯。这就像一个成年礼。

您得到 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";

Here's a demo