note 96699 deleted from function.date-default-timezone-set by danbrown

From: Date: Fri, 12 Mar 2010 02:30:03 +0000
Subject: note 96699 deleted from function.date-default-timezone-set by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166941@lists.php.net to get a copy of this message
Note Submitter: Vincent Arovola ---- Wow Thanks Frederick! I was having the same problem and surprised there's no simpler way to do it. Your code helped me a lot, but I noticed it was breaking if local time is a different day from UTC. Thought I'd share my modification along with thanks. function setTimezoneForOffset($offset) { $abbrarray = timezone_abbreviations_list(); $setit = false; date_default_timezone_set("UTC"); $utcbits = localtime(time(), true); foreach ($abbrarray as $abbr) { foreach ($abbr as $city) { $thistimezone = $city['timezone_id']; date_default_timezone_set($thistimezone); $localbits = localtime(time(), true); if($utcbits["tm_yday"] == $localbits["tm_yday"]) { // WE'RE STILL THE SAME DAY, e.g. SF NOON, LONDON 8PM $thisoffset = $localbits["tm_hour"] - $utcbits["tm_hour"]; // e.g. 12 - 20 = -8 } else if($utcbits["tm_yday"] > $localbits["tm_yday"]) { // OUR TIME IS DAY BEFORE UTC, e.g. SF 10PM, LONDON 6AM $thisoffset = $localbits["tm_hour"] - (24 + $utcbits["tm_hour"]); // e.g. 22 - (24 + 6) = -8 } else if($utcbits["tm_yday"] < $localbits["tm_yday"]) { // OUR TIME IS DAY AFTER UTC, e.g. LONDON 10PM, INDIA 3:30AM $thisoffset = (24 + $localbits["tm_hour"]) - $utcbits["tm_hour"]; // e.g. (24 + 3.5) - 22 = +5.5 } if($thisoffset == $offset) { $setit = true; break; } } if($setit) break; } }

« previous php.notes (#166941) next »