I'm being told that this below method of calculating the user's local time is sometimes not working. What's the best way to do this in PHP? What do you do?

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;
}

Accepted Answer

Use the DateTime extension, such as DateTime::getOffset,
or DateTimeZone::getOffset

PS: some countries might have perform several timezone update,
this method DateTimeZone::getTransitions reveal the transition history

Written by ajreal
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki