PHP Doku:: Gibt den Namen der Zeitzonenabkürzung zurück - function.timezone-name-from-abbr.html

Verlauf / Chronik / History: (50) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatums- und zeitrelevante ErweiterungenDatum und UhrzeitDatum/Uhrzeit Funktionentimezone_name_from_abbr

Ein Service von Reinhard Neidl - Webprogrammierung.

Datum/Uhrzeit Funktionen

<<timezone_location_get

timezone_name_get>>

timezone_name_from_abbr

(PHP 5 >= 5.1.3)

timezone_name_from_abbrGibt den Namen der Zeitzonenabkürzung zurück

Beschreibung

string timezone_name_from_abbr ( string $abbr [, int $gmtOffset = -1 [, int $isdst = -1 ]] )

Parameter-Liste

abbr

Zeitzonenabkürzung.

gmtOffset

Offset zu GMT in Sekunden. Standardwert ist -1, das bedeutet, dass die erste gefundene Zeitzone, die abbr entspricht, zurückgegeben wird. Andernfalls wird der exakte Offset gesucht und nur, wenn dieser nicht gefunden wird, wird die erste Zeitzone mit einem Offset zurückgegeben.

isdst

Sommerzeit-Indikator. Wenn abbr nicht existiert, wird die Zeitzone ausschließlich anhand von offset und isdst gesucht.

Rückgabewerte

Gibt im Erfolgsfall den Namen der Zeitzone zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 Ein timezone_name_from_abbr()-Beispiel

<?php
echo timezone_name_from_abbr("CET") . "\n";
echo 
timezone_name_from_abbr(""36000) . "\n";
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Europe/Berlin
Europe/Paris

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
Master Tablu
24.02.2009 16:03
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"
?>
chris at mretc dot net
11.11.2008 2:25
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;
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)