timezone_name_from_abbr

(PHP 5 >= 5.1.3, PHP 7, PHP 8)

timezone_name_from_abbrRetorna um nome de fuso horário através da abreviatura e do deslocamento UTC

Descrição

timezone_name_from_abbr(string $abbr, int $utcOffset = -1, int $isDST = -1): string|false

Parâmetros

abbr

A abreviação do fuso horário.

utcOffset

Deslocamento ao Horário de Greenwish(GMT) em segundos. Sendo -1 o padrão, que significa que o primeiro fuso horário correspondente encontrado com abbr é retornado. Caso contrário o deslocamento é buscao, e caso não encontrado, o o primeiro fuso horário, com qualquer deslocamento, é retornado.

isDST

Indicador do horário de verão. O padrão é -1, e informa que tendo o fuso horário horário de verão, ou não, este não será levado em consideração na busca. Se configurado para 1, então utcOffset é utilizado como diferença no horário de verão; se 0, então utcOffset é utilizado sem a diferença do horário de verão. Se abbr não existir, o fuso horário será buscado exclusivamente pelo utcOffset e isDST.

Valor Retornado

Retorna o fuso horário em caso de sucesso ou false em caso de falha.

Exemplos

Exemplo #1 Um exemplo da função timezone_name_from_abbr()

<?php
echo timezone_name_from_abbr("CET") . "\n";
echo
timezone_name_from_abbr("", 3600, 0) . "\n";
?>

O exemplo acima produzirá algo semelhante a:

Europe/Berlin
Europe/Paris

Veja Também

add a note add a note

User Contributed Notes 4 notes

up
9
chris at mretc dot net
15 years ago
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;
}
?>
up
2
Jon Stovell
7 years ago
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
?>
up
2
Master Tablu
15 years ago
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"
?>
up
-1
atrauzzi at gmail dot com
8 years ago
Fun fact: (60*60) * -2 always seems to return null.

Perhaps because there's no timezone that corresponds to UTC -2.
To Top