PHP实战中知识总结 / 日期相关的类 - DateTimeZone类

DateTimeZone 表示时区的类。

DateTimeZone {
 /* 常量*/
 const integer AFRICA = 1 ;   // 非洲时区
 const integer AMERICA = 2 ;  // 美国时区
 const integer ANTARCTICA = 4 ; // 南极洲时区
 const integer ARCTIC = 8 ;   // 北极时区
 const integer ASIA = 16 ;   // 亚洲时区
 const integer ATLANTIC = 32 ; // 大西洋时区
 const integer AUSTRALIA = 64 ; // 澳大利亚时区
 const integer EUROPE = 128 ;  // 欧洲时区
 const integer INDIAN = 256 ;  // 印度时区
 const integer PACIFIC = 512 ; // 太平洋时区
 const integer UTC = 1024 ;   // UTC时区
 const integer ALL = 2047 ;   // 所有时区
 const integer ALL_WITH_BC = 4095 ;// 所有时区,包括向后兼容
 const integer PER_COUNTRY = 4096 ;
 /* 方法 */
 public __construct ( string $timezone )
 public getLocation ( void ) : array
 public getName ( void ) : string
 public getOffset ( DateTimeInterface $datetime ) : int
 public getTransitions ([ int $timestamp_begin = PHP_INT_MIN [, int $timestamp_end = PHP_INT_MAX ]] ) : array
  // getTransitions 方法返回时区的所有转换
 public static listAbbreviations ( void ) : array
  //listAbbreviations 方法返回包含dst、偏移量和时区名称的关联数组
 public static listIdentifiers ([ int $what = DateTimeZone::ALL [, string $country = NULL ]] ) : array
  // listIdentifiers 方法返回包含所有定义的时区标识符的数字索引数组
}
$date = new DateTime;
// DateTime类的获取时区方法,返回DateTimeZone对象
$time_zone = $date->getTimezone();
dump($time_zone);
object(DateTimeZone)#227 (2) {
 ["timezone_type"] => int(3)
 ["timezone"] => string(13) "Asia/Shanghai"
}
// DateTimeZone对象的getLocation方法:返回时区的位置信息
$location = $time_zone->getLocation();
dump($location);
array(4) {
 ["country_code"] => string(2) "CN"
 ["latitude"] => float(31.23333)
 ["longitude"] => float(121.46666)
 ["comments"] => string(12) "Beijing Time"
}
// DateTimeZone对象的getOffset方法:返回时区的偏移量
$offset = $date->getOffset();
dump($offset);
// 实例化DateTimeZone类时,需要时区字符串参数
$date_time_zone = new DateTimeZone('Asia/Shanghai');

PHP实战中知识总结