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;
}
Use the DateTime extension, such as DateTime::getOffset,
or DateTimeZone::getOffset
Some countries might have perform several timezone update,
this method DateTimeZone::getTransitions reveal the transition history
returns the UTC offset in seconds.
A quick solution:
Just answered a very similar question over here. I recommend you check that one out; I explained the two preferred ways of doing timezone offset calculation (using simple math, and then the
datetimezone
anddatetime
classes) pretty thoroughly.