`execution_time()`函数的时间错误 [英] Wrong time for `execution_time()` function

查看:86
本文介绍了`execution_time()`函数的时间错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试计算执行时间。
但是问题是执行时间少于1小时时,它会写错时间戳,例如:

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

仅持续8秒,为什么会出现6个小时?为什么会发生这种情况以及如何解决呢?

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

推荐答案

由于时区不同,您会相差6个小时。

You get 6 hours difference because of your timezone.

date_default_timezone_set('UTC');

线路,将时区设置为UTC并消除差异。

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

这篇关于`execution_time()`函数的时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆