简体   繁体   English

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

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

I get a time in seconds from my database (stored as an integer):我从我的数据库中得到一个以秒为单位的时间(存储为整数):

$time = $data["USER_TIME"];

And then i do the following:然后我执行以下操作:

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

And i create a time string after:我在之后创建了一个时间字符串:

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

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

But the results are confusing for me:但结果让我感到困惑:

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

Can someone explain me what is happening here?有人可以解释我这里发生了什么吗?

Var_dump $data["USER_TIME"] : var_dump $data["USER_TIME"] :

10 
15
20 
25
30
35
40
45
50

Let's try it with floor .让我们用floor试试。 You are using round now, which means everything equal to or above .5 becomes the next integer.您现在正在使用round ,这意味着等于或大于 0.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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM