Wrong time for `execution_time()` function [duplicate]

可紊 提交于 2019-12-08 10:56:06

问题


public function execution_time($begin = null)
{
    if($begin == null){
        return microtime(true);
    }
    else {
        $result = date("H:i:s", microtime(true) - $begin);
        return $result;
    }
}

I'm trying to count the execution time. But the problem is when execution time is lesser then 1 hour, it writes wrong timestamp, for example:

$timeBegin = execution_time();
sleep(8);
$time_end = execution_time($timeBegin); // it shows 06:00:08

It lasts only 8 seconds, why 6 hours are appear? Why this happens and how to solve this?


回答1:


You get 6 hours difference because of your timezone.

date_default_timezone_set('UTC');

Line, sets time zone to UTC and eliminates the difference.




回答2:


You need to set the default timezone in your script:

date_default_timezone_set

Here is the working example

https://codebrace.com/editor/b16b092fb



来源:https://stackoverflow.com/questions/52075941/wrong-time-for-execution-time-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!