PHP: round() 函数中发生了什么?

PHP: What is happening in the round() function?

我从我的数据库中得到一个以秒为单位的时间(存储为一个整数):

$time = $data["USER_TIME"];

然后我执行以下操作:

$hours = round(($time / 3600), 0);
$minutes = round((($time - ($hours * 3600)) / 60), 0);
$seconds = $time - ($hours * 3600) - ($minutes * 60);

然后我创建了一个时间字符串:

$timeString = formatNumber($hours).":".formatNumber($minutes).":".formatNumber($seconds);

function formatNumber($number) {
    if($number < 10) {
        return ("0".$number);
    } else {
        return ("".$number);    
    }
}

但结果让我感到困惑:

10 seconds -> 00:00:10
15 seconds -> 00:00:15
20 seconds -> 00:00:20
25 seconds -> 00:00:25
30 seconds -> 00:01:-30
35 seconds -> 00:01:-25
40 seconds -> 00:01:-20
45 seconds -> 00:01:-15
50 seconds -> 00:01:-10

谁能给我解释一下这里发生了什么?

Var_dump $数据["USER_TIME"] :

10 
15
20 
25
30
35
40
45
50

让我们用 floor 试试看。您现在正在使用 round,这意味着等于或大于 .5 的所有内容都将成为下一个整数。

$hours = floor($time / 3600);
$minutes = floor(($time - ($hours * 3600)) / 60);
$seconds = $time - ($hours * 3600) - ($minutes * 60);

您可以使用 gmdate() 函数代替 floor()round()

echo gmdate("H:i:s", 685);