timezone_name_from_abbr
(PHP 5 >= 5.1.3)
timezone_name_from_abbr — Returns the timezone name from abbreviation
Description
$abbr
[, int $gmtOffset
= -1
[, int $isdst
= -1
]] )Parameters
-
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, thengmtOffset
is assumed to be an offset without daylight saving in effect. Ifabbr
doesn't exist then the time zone is searched solely by thegmtOffset
andisdst
.
Return Values
Returns time zone name on success or FALSE
on failure.
Examples
Example #1 A timezone_name_from_abbr() example
<?php
echo timezone_name_from_abbr("CET") . "\n";
echo timezone_name_from_abbr("", 3600, 0) . "\n";
?>
The above example will output something similar to:
Europe/Berlin Europe/Paris
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с датой и временем
- Дата и Время
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
Коментарии
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
/* Takes a GMT offset (in hours) and returns a timezone name */
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;
}
?>
Another way to do this is to wrap the function in a class that extends the DateTimeZone class:
<?php
/**
* Helps with timezones.
* @link http://us.php.net/manual/en/class.datetimezone.php
*
* @package Date
*/
class Helper_DateTimeZone extends DateTimeZone
{
/**
* Converts a timezone hourly offset to its timezone's name.
* @example $offset = -5, $isDst = 0 <=> return value = 'America/New_York'
*
* @param float $offset The timezone's offset in hours.
* Lowest value: -12 (Pacific/Kwajalein)
* Highest value: 14 (Pacific/Kiritimati)
* @param bool $isDst Is the offset for the timezone when it's in daylight
* savings time?
*
* @return string The name of the timezone: 'Asia/Tokyo', 'Europe/Paris', ...
*/
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"
?>
Fun fact: (60*60) * -2 always seems to return null.
Perhaps because there's no timezone that corresponds to UTC -2.
In some cases, timezone_name_from_abbr() may return a "historical" (i.e. deprecated) timezone identifier rather than the current standard one for a given location. For example:
<?php
echo timezone_name_from_abbr('EASST'); // prints "Chile/EasterIsland" instead of "Pacific/Easter"
?>
This means that the returned timezone identifier is not guaranteed to be in the results of timezone_identifiers_list() unless you include the "backwards compatible" timezones.
<?php
var_dump(in_array(timezone_name_from_abbr('EASST'), timezone_identifiers_list())); // (bool)false
var_dump(in_array(timezone_name_from_abbr('EASST'), timezone_identifiers_list(DateTimeZone::ALL_WITH_BC))); // (bool)true
?>