timezone_open()函數(shù)創(chuàng)建新的DateTimeZone對(duì)象
timezone_open()函數(shù)是DateTimeZone::__ construct()的別名。 它接受時(shí)區(qū)字符串作為參數(shù),并創(chuàng)建一個(gè)DateTimeZone對(duì)象。
timezone_open($timezone)
序號(hào) | 參數(shù)及說(shuō)明 |
---|---|
1 | timezone (必需) 這是表示時(shí)區(qū)的字符串值。 |
timezone_name_get()函數(shù)返回DateTimeZone對(duì)象。如果失敗,則此函數(shù)返回布爾值false。
此函數(shù)最初是在PHP版本5.2.0中引入的,并且可以在所有更高版本中使用。
以下示例演示了timezone_open()函數(shù)的用法-
<?php $tz = "Indian/mahe"; $res = timezone_open($tz); print_r($res); ?>測(cè)試看看?/?
輸出結(jié)果
DateTimeZone Object ( [timezone_type] => 3 [timezone] => Indian/mahe )
創(chuàng)建一個(gè)新的 DateTimeZone 對(duì)象,然后返回時(shí)區(qū)的名稱:
<?php $dateSrc = '2017-06-25 1:50 GMT'; $dateTime = date_create( $dateSrc); $DateTimeZone = timezone_open ( 'America/Chicago' ); date_timezone_set( $dateTime, $DateTimeZone ); $NewDateTimeZone = date_timezone_get($dateTime); echo '新時(shí)區(qū)為 '. timezone_name_get($NewDateTimeZone); echo "\n"; # 使用第二種方法 $dateTime = new DateTime($dateSrc); $DateTimeZone = new DateTimeZone( 'America/Chicago' ); $dateTime->setTimezone( $DateTimeZone ); $NewDateTimeZone = $dateTime->getTimezone (); echo '新時(shí)區(qū)為 '. timezone_name_get ($NewDateTimeZone); ?>測(cè)試看看?/?
輸出結(jié)果:
新時(shí)區(qū)為 America/Chicago 新時(shí)區(qū)為 America/Chicago