©
このドキュメントでは、 php中国語ネットマニュアル リリース
(PHP 5 >= 5.1.3)
timezone_name_from_abbr — Returns the timezone name from abbreviation
$abbr
[, int $gmtOffset
= -1
[, int $isdst
= -1
]] )
abbr
Time zone abbreviation.
gmtOffset
Offset from GMT in seconds. Defaults to -1 which means that first found
time zone corresponding to abbr
is returned.
Otherwise exact offset is searched and only if not found then the first
time zone with any offset is returned.
isdst
Daylight saving time indicator. Defaults to -1, which means that
whether the time zone has daylight saving or not is not taken into
consideration when searching. If this is set to 1, then the
gmtOffset
is assumed to be an offset with
daylight saving in effect; if 0, then gmtOffset
is assumed to be an offset without daylight saving in effect. If
abbr
doesn't exist then the time zone is
searched solely by the gmtOffset
and
isdst
.
Returns time zone name on success 或者在失败时返回 FALSE
.
Example #1 A timezone_name_from_abbr() example
<?php
echo timezone_name_from_abbr ( "CET" ) . "\n" ;
echo timezone_name_from_abbr ( "" , 3600 , 0 ) . "\n" ;
?>
以上例程的输出类似于:
Europe/Berlin Europe/Paris
[#1] Master Tablu [2009-02-24 07:03:51]
Another way to do this is to wrap the function in a class that extends the DateTimeZone class:
<?php
class Helper_DateTimeZone extends DateTimeZone
{
final public static function tzOffsetToName($offset, $isDst = null)
{
if ($isDst === null)
{
$isDst = date('I');
}
$offset *= 3600;
$zone = timezone_name_from_abbr('', $offset, $isDst);
if ($zone === false)
{
foreach (timezone_abbreviations_list() as $abbr)
{
foreach ($abbr as $city)
{
if ((bool)$city['dst'] === (bool)$isDst &&
strlen($city['timezone_id']) > 0 &&
$city['offset'] == $offset)
{
$zone = $city['timezone_id'];
break;
}
}
if ($zone !== false)
{
break;
}
}
}
return $zone;
}
}
?>
Then you could do something like this:
<?php
$Dtz = new Helper_DateTimeZone(Helper_DateTimeZone::tzOffsetToName(-5));
var_dump($Dtz->getName());
string(16) "America/New_York"
?>
[#2] chris at mretc dot net [2008-11-10 17:25:29]
timezone_name_from_abbr() sometimes returns FALSE instead of an actual timezone: http://bugs.php.net/44780
It's possible to workaround it for some cases by getting the timezone name from timezone_abbreviations_list(). For example, if you have the GMT offset and want a timezone name:
<?php
function tz_offset_to_name($offset)
{
$offset *= 3600; // convert hour offset to seconds
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
foreach ($abbr as $city)
{
if ($city['offset'] == $offset)
{
return $city['timezone_id'];
}
}
}
return FALSE;
}
?>