2013-01-06 54 views
-2

怎麼去用PHP在Windows服務器上的服務器時區?PHP - 如何獲得Windows服務器時區?

對於Linux:

$systemTimeZone = system('date +%Z'); 

的* nix系統:

$systemTimeZone = popen("date +%Z"); 

窗口:

$systemTimeZone = ??? 

greez & THX, 天空...

編輯: 我知道像PHP函數:

date_default_timezone_get() 
date('T') 
\DateTime::timezone 

,但所有來自PHP這個內置的功能是referenz在php.ini中設置。我想知道從操作系統中真正使用的時區。

我必須嘗試使用​​Windows命令行工具

systeminfo 

但這是緩慢的通話和解析。另一種命令行工具,我有testet是:

tzutil /g 

這不是時區格式我想要的東西,在我的情況下,我得到這樣的:

$:> tzutil /g 
W. Europe Standard Time 

任何其他的想法給我嗎?

回答

0

這會告訴你的服務器時區縮寫:

$systemTimeZone = date("T"); 
echo $systemTimeZone; 

例如EST,MDT ...

+1

TZ abbriviations在世界各地都是模棱兩可的。請參閱http://www.timeanddate.com/library/abbreviations/timezones/ –

1

我剛纔同樣的錯誤,並沒有發現更好的解決方案,然後用自制的映射。對於我使用過的窗口:

// this array can be filled up with whatever you need 
$timezones = array(
     'GMT' => 'Europe/London' 
    , '0' => 'Europe/London' 
    , '1' => 'Europe/London' 
    , 'GMT Standard Time' => 'Europe/London' 
    , '2' => 'Europe/Berlin' 
    , 'W. Europe Standard Time' => 'Europe/Berlin' 
); 

// getting the timezone  
$timezone = exec('tzutil /g'); 

// and setting it accordingly 
if(array_key_exists($timezone, $timezones)) {  
    echo("> timezone identified as " . $timezones[$timezone] . "\n"); 
    date_default_timezone_set($timezones[$timezone]); 
} else { 
    die("Unknown Timezone: " . $timezone); 
}