DateTimeZone
在线手册:中文  英文

DateTimeZone::listAbbreviations

timezone_abbreviations_list

(PHP 5 >= 5.2.0)

DateTimeZone::listAbbreviations -- timezone_abbreviations_listReturns associative array containing dst, offset and the timezone name

说明

面向对象风格

public static array DateTimeZone::listAbbreviations ( void )

过程化风格

返回值

Returns array on success 或者在失败时返回 FALSE.

范例

Example #1 A timezone_abbreviations_list() example

<?php
$timezone_abbreviations 
DateTimeZone::listAbbreviations();
print_r($timezone_abbreviations["acst"]);
?>

以上例程的输出类似于:

Array
(
    [0] => Array
        (
            [dst] => 1
            [offset] => -14400
            [timezone_id] => America/Porto_Acre
        )

    [1] => Array
        (
            [dst] => 1
            [offset] => -14400
            [timezone_id] => America/Eirunepe
        )

    [2] => Array
        (
            [dst] => 1
            [offset] => -14400
            [timezone_id] => America/Rio_Branco
        )

    [3] => Array
        (
            [dst] => 1
            [offset] => -14400
            [timezone_id] => Brazil/Acre
        )

)

参见


DateTimeZone
在线手册:中文  英文

用户评论:

the_dark_lord12001 at yahoo dot com (2010-08-30 20:51:28)

With the new dateTime functionality of PHP 5, i found that the dateTime::listabreviations and the dateTime::listIdentifiers return invalid time zone continent/city pairs that throws an error saying time zone not found in data base. So i wrote a class function that will return the abbreviated valid time zones

I hope this helps someone, and i would appreciate some tips on how to improve this class.

<?php
class TimeZones
{
    public 
$zones = array();
    public 
$countries = array();
    
    
/**
     * Empty construct
     */
    
public function __construct(){}
    
    
/**
     * Utility function to re-index the array returned
     * from getTimeZones(). The $a is integer indexed
     * from the order of DateTimeZone::listIdentifiers()
     * so we need to reindex it to match proper formating
     *
     * @param array;
     * @return new indexed array;
     */
    
protected function cmp $array )
    {
        
$i=0;
    
        if ( !
is_array $array ) ) return;
        
        foreach ( 
$array as $key => $val )
        {
            if ( 
is_array $val ) )
            {
                foreach ( 
$val as $k => $v )
                {
                    if ( 
is_array $v ) )
                    {
                        foreach ( 
$v as $a => $b )
                        {
                            if ( 
is_array $b ) )
                            {
                                foreach ( 
$b as $z => $y )
                                {
                                    if ( 
$newK != $k $i=0;
                                        
                                    
$arr[$key][$k][$i][$z] = $y;
                                    
$newK $k;
                                }
                            
$i++;
                            }
                        }
                    }
                }
            }
        }
        return 
$arr;
    }
    
    
/**
     * This function takes an array of the old style of time zone
     * abreviations. i.e. CST, EST, PST
     *
     * @param array;
     * @return none; sets $this->zones variable
     */
    
public function setTimeZones $array )
    {
        if ( !
is_array($array) ) return;
        
        
$this->zones $array;
    }
    
    
/**
     * This function takes an array of the continent names for which
     * you want to get the valid time zone names for
     *
     * @param array;
     * @return none; sets $this->countries variable
     */
    
public function setCountries $array )
    {
        if ( !
is_array($array) ) return;
        
        
$this->countries $array;
    }
    
    
/**
     * This function is similiar to the dateTime::listabbreviations method
     * but this one weeds out those time zones that are not recognized.
     *
     * @param none;
     * @return array; a multidimensional [country][time zone abreviation][city/offset]
     */
    
public function getTimeZones()
    {
        
$ident DateTimeZone::listIdentifiers();
        
$cntIdent count($ident);
        
        
$zones $this->zones;
        
$cntZones count($zones);
        
        for ( 
$i=0$i<$cntZones$i++ )
        {
            for ( 
$j=0$j<$cntIdent$j++ )
            {
                if ( 
date_default_timezone_set($ident[$j]) )
                {
                    try
                    {
                        if ( !
$date = new dateTime($ident[$j]) )
                        {
                            throw new 
Exception ($e);
                        }
                        else
                        {
                            
$zoneFormat $date->format('T');
                            
$tz $date->getTimezone();
                            
$tzName $tz->getName();
                            
$offset $date->getOffset();
                            
                            
$utc[$j] = array ( 'city' => $tzName'offset' => $offset );
                            
                            if ( 
strtoupper($zones[$i]) == $zoneFormat )
                            {
                                
$ex explode '/'$tzName );
                                
sort($this->countries);
                                
                                for ( 
$z=0$z<count($this->countries); $z++ )
                                {
                                    if ( 
$ex[0] == ucwords($this->countries[$z]) )
                                    {
                                        
$array[$ex[0]][$zoneFormat][$j] = array ( 'city' => $tzName'offset' => $offset );
                                    }
                                }
                            }
                        }
                    }
                    catch(
Exception $e) {}
                }
            }
        }
        
        
$arr $this->cmp($array);
        return 
$arr;
    }
}

$class = new TimeZones();

$array = array ( 'cdt''edt''mst''pdt' );
$country = array ('America');
$class->setCountries($country);
$class->setTimeZones($array);

print_r($class->getTimeZones());
?>

kingskippus at gmail dot com (2009-04-04 08:32:38)

Note that the dst field is of boolean type, so if you are doing an identity comparison, you need to test for true or false, not 0 or 1.  For example:

<?php
  $timezone_abbreviations 
DateTimeZone::listAbbreviations();
  foreach (
$timezone_abbreviations["est"] as $tz) {
    echo 
$tz['timezone_id'];
    
// if ($tz['dst'] === 1) will always evaluate to false
    
if ($tz['dst'] === true) {
      echo 
" (DST observed)<br />\n";
    }
    
// Could use else here, but for illustration...
    
if ($tz['dst'] === false) {
      echo 
" (DST not observed)<br />\n";
    }
  }
?>

易百教程