note 86950 modified in function.date-default-timezone-set by thiago
| From: | thiago@php.net | Date: | Tue, 29 Dec 2009 13:09:11 +0000 |
| Subject: | note 86950 modified in function.date-default-timezone-set by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164423@lists.php.net to get a copy of this message | ||
While it's easy to change timezones based on names or abbreviations, I haven't found any
straightforward way of doing so using an offset integer. This situation comes up if you're
using AJAX to get information about a user's timezone; javascript's getTimezoneOffset()
method just sends you an offset number. So, here's my clunky solution: an adaptation of
chris' function at http://us.php.net/manual/en/function.timezone-name-from-abbr.php.
<?php
function set_tz_by_offset($offset) {
$offset = $offset*60*60;
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr) {
//echo $abbr."<br>";
foreach ($abbr as $city) {
//echo $city['offset']." $offset<br>";
if ($city['offset'] == $offset) { // remember to multiply $offset
by -1 if you're getting it from js
date_default_timezone_set($city['timezone_id']);
return true;
}
}
}
date_default_timezone_set("ust");
return false;
}
set_tz_by_offset(-1);
?>
[EDIT BY thiago AT php DOT net: This code has contributions from iateadonut at gmail dot com]
--was--
While it's easy to change timezones based on names or abbreviations, I haven't found any
straightforward way of doing so using an offset integer. This situation comes up if you're
using AJAX to get information about a user's timezone; javascript's getTimezoneOffset()
method just sends you an offset number. So, here's my clunky solution: an adaptation of
chris' function at http://us.php.net/manual/en/function.timezone-name-from-abbr.php.
<?php
function set_tz_by_offset($offset) {
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr) {
foreach ($abbr as $city) {
if ($city['offset'] == $offset) { // remember to multiply $offset by -1
if you're getting it from js
date_default_timezone_set($city['timezone_id']);
return true;
}
}
}
date_default_timezone_set("ust");
return false;
}
?>
http://php.net/manual/en/function.date-default-timezone-set.php