note 96551 modified in function.date-default-timezone-set by danbrown
| From: | danbrown@php.net | Date: | Thu, 04 Mar 2010 17:53:12 +0000 |
| Subject: | note 96551 modified in function.date-default-timezone-set by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166667@lists.php.net to get a copy of this message | ||
It's really a hassle that there is no way to set the timezone offset. I attempted to use the
code that searched for the correct timezone name by offset from below. I found it didn't work
reliably since various cities may have different rules about time DST etc. My solution is to
actually search the array and do a test to insure the offset is what I expect it to be. It seems
very kludgey (if thats a word) but it works.
<?php
function setTimezoneByOffset($offset)
{
$testTimestamp = time();
date_default_timezone_set('UTC');
$testLocaltime = localtime($testTimestamp,true);
$testHour = $testLocaltime['tm_hour'];
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
//echo $abbr."<br>";
foreach ($abbr as $city)
{
date_default_timezone_set($city['timezone_id']);
$testLocaltime = localtime($testTimestamp,true);
$hour = $testLocaltime['tm_hour'];
$testOffset = $hour - $testHour;
if($testOffset == $offset)
{
return true;
}
}
}
return false;
}
?>
--was--
It's really a hassle that there is no way to set the timezone offset. I attempted to use the
code that searched for the correct timezone name by offset from below. I found it didn't work
reliably since various cities may have different rules about time DST etc. My solution is to
actually search the array and do a test to insure the offset is what I expect it to be. It seems
very kludgey (if thats a word) but it works.
function setTimezoneByOffset($offset)
{
$testTimestamp = time();
date_default_timezone_set('UTC');
$testLocaltime = localtime($testTimestamp,true);
$testHour = $testLocaltime['tm_hour'];
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
//echo $abbr."<br>";
foreach ($abbr as $city)
{
date_default_timezone_set($city['timezone_id']);
$testLocaltime = localtime($testTimestamp,true);
$hour = $testLocaltime['tm_hour'];
$testOffset = $hour - $testHour;
if($testOffset == $offset)
{
return true;
}
}
}
return false;
}
http://php.net/manual/en/function.date-default-timezone-set.php