PHP 中如何处理时区差异计算?

Web*_*net 5 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;
}
Run Code Online (Sandbox Code Playgroud)

ajr*_*eal 2

使用 DateTime 扩展,例如DateTime::getOffsetDateTimeZone::
getOffset

有些国家可能执行了多次时区更新,
此方法DateTimeZone::getTransitions揭示了转换历史记录