php - 你如何处理 PHP 中的时差计算?

标签 php timezone

有人告诉我,下面这种计算用户本地时间的方法有时不起作用。在 PHP 中执行此操作的最佳方法是什么?你是做什么的?

public function getTimeOffset ($time) {
    $this->_cacheTimeOffset();  
    if ($this->timeOffsetExecuted) {
        $d = date("O");
        $neg = 1;

        if (substr($d, 0, 1) == "-") {
            $neg = -1;
            $d = substr($d, 1);
        }

        $h = substr($d, 0, 2)*3600;
        $m = substr($d, 2)*60;

        return $time + ($neg * ($h + $m) * -1) + (($this->timeOffset + $this->getDstInUse()) * 3600);
    }
    return $time;
}

最佳答案

使用日期时间扩展,例如DateTime::getOffset ,
DateTimeZone::getOffset

一些国家可能已经执行了多个时区更新,
这个方法DateTimeZone::getTransitions揭示过渡历史

关于php - 你如何处理 PHP 中的时差计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4299183/

相关文章:

php - PHP上的表情符号错误

php - SplSubject/SplObserver有何用处?

javascript - 如何使用 FormData 将输入值传递给 ajax?

php - 在一个页面中显示两次 laravel 分页链接

python - 如何在 python 中使用带有 datetime 对象的时区?

c - tm 结构转换为错误的 time_t

php - 如何在 Zend Framework 2 中配置学说扩展?

android - 如何防止我的应用程序获取 TimeZone.getDefault() 的缓存值?

.net - 测量耗时会计日光节约

iphone - Objective-c 如何在 GMT 时区获取没有时间分量的日期?