note 96700 added to function.date-default-timezone-set

From: Date: Fri, 12 Mar 2010 02:18:49 +0000
Subject: note 96700 added to function.date-default-timezone-set
Groups: php.notes 
Request: Send a blank email to php-notes+get-166937@lists.php.net to get a copy of this message
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; } } ---- Server IP: 208.69.120.55 Probable Submitter: 24.6.151.152 ---- Manual Page -- http://www.php.net/manual/en/function.date-default-timezone-set.php Edit -- https://master.php.net/note/edit/96700 Del: integrated -- https://master.php.net/note/delete/96700/integrated Del: useless -- https://master.php.net/note/delete/96700/useless Del: bad code -- https://master.php.net/note/delete/96700/bad+code Del: spam -- https://master.php.net/note/delete/96700/spam Del: non-english -- https://master.php.net/note/delete/96700/non-english Del: in docs -- https://master.php.net/note/delete/96700/in+docs Del: other reasons-- https://master.php.net/note/delete/96700 Reject -- https://master.php.net/note/reject/96700 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166937) next »