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;
}
?>
date_default_timezone_set
(PHP 5 >= 5.1.0)
date_default_timezone_set — 设定用于一个脚本中所有日期时间函数的默认时区
说明
bool date_default_timezone_set
( string $timezone_identifier
)
date_default_timezone_set() 设定用于所有日期时间函数的默认时区。
Note: 自 PHP 5.1.0 起(此版本日期时间函数被重写了),如果时区不合法则每个对日期时间函数的调用都会产生一条 E_NOTICE 级别的错误信息,如果使用系统设定或 TZ 环境变量则还会产生 E_STRICT 级别的信息。
返回值
如果 timezone_identifier 参数无效则返回 FALSE,否则返回 TRUE。
更新日志
| 版本 | 说明 |
|---|---|
| 5.1.2 | 本版本开始验证 timezone_identifier 参数。 |
date_default_timezone_set
jason at jasonpriem dot com
11-Nov-2008 08:29
11-Nov-2008 08:29
Anonymous
27-Sep-2008 10:34
27-Sep-2008 10:34
If you have problems with errors, why the default is not set use this on the top of the script:
<?php
if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get"))
@date_default_timezone_set(@date_default_timezone_get());
?>
Rob Kaper
15-Jul-2008 01:46
15-Jul-2008 01:46
If you want users to choose their own timezones, here's some code that gets all available timezones but only uses one city for each possible value:
<?php
$timezones = DateTimeZone::listAbbreviations();
$cities = array();
foreach( $timezones as $key => $zones )
{
foreach( $zones as $id => $zone )
{
/**
* Only get timezones explicitely not part of "Others".
* @see http://www.php.net/manual/en/timezones.others.php
*/
if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $zone['timezone_id'] ) )
$cities[$zone['timezone_id']][] = $key;
}
}
// For each city, have a comma separated list of all possible timezones for that city.
foreach( $cities as $key => $value )
$cities[$key] = join( ', ', $value);
// Only keep one city (the first and also most important) for each set of possibilities.
$cities = array_unique( $cities );
// Sort by area/city name.
ksort( $cities );
?>
php_manual at lk2 dot de
12-Aug-2007 01:37
12-Aug-2007 01:37
@davidn at datalinktech dot com dot au
set_default_timezone() has no effect at all on how apache logs are timestamped (at least for me)
[red. that's untrue if you set the TZ env var... that will affect Apache as well - Derick]
It is however true, that all dates and times that php formats that are _not_ timestamps will be in that timezone.
Timestamps are always GMT
PeerGoal.com
12-Feb-2007 04:21
12-Feb-2007 04:21
The problem:
date() [function.date]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Los_Angeles' for 'PST/-8.0/no DST' instead
Of course this is a problem that recently surfaced since PHP5. Quick fix is to set your time zone, add this line to your php code:
date_default_timezone_set("America/Los_Angeles");
davidn at datalinktech dot com dot au
21-Dec-2006 05:27
21-Dec-2006 05:27
Note that there may be some unexpected side-effects that result from using either set_default_timezone() or the putenv("TZ=...") workalike for earlier PHP versions. ANY date formatted and output either by PHP or its apache host process will be unconditionally expressed in that timezone.
[red. That is only true for the putenv() hack - Derick]
This does indeed include the web server's logs and other output files and reports which by default usually do not include any indication of timezone. This has a further side-effect on log processing and analysis, obviously.
