2012-11-05 27 views
1

嗨配合,我如何使用PHP

轉換時區字符串轉換成標準時區格式我有一個時區字符串,如:

Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)

需要把這些字符串轉換爲以下標準時區格式使用php

(GMT-06:00)-Central America

代碼

echo $tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)"; 

echo $tz2 = substr($tz, 0, 34); 

echo date("P", strtotime("$tz2")); 

結果

Mon Nov 05 2012 06:52:37 GMT-0600 (Central Standard Time) 

Mon Nov 05 2012 06:52:37 GMT-0600 

+00:00 

試圖參考:Date

但不會產生這樣的格式。現在需要幫助的隊友,感謝...

+0

你對時區格式有任何控制權? – vascowhite

回答

1

新編輯 - 試試這個...

$tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)"; 

$time = explode(" ",$tz); 
$timezone = explode("(", str_replace(")","",$tz)); // would be better to use a regex 
$new_time = "(".$time[5].")-".$timezone[1]; 

echo $new_time; // gives (GMT-0600)-Central America Standard Time 

退出之前舊代碼.....

$tz = "Mon Nov 05 2012 06:54:06 GMT-0600 (Central America Standard Time)"; 
$new_time = strtotime($tz); 

$new_date = date('(P)-e', $new_time); 
echo $new_date; 

(-05:00)美合資/紐約

+0

它顯示像這樣:'(+00:00)-UTC',因此時區在' localhost' – Frank

+0

檢查上面的代碼,我已經更新它。它只會在日期信息串是相同的佈局 – Chris

+0

有任何喜悅嗎? – Chris

1

echo date("P", strtotime("$tz2"));

呼應

GMT-0600

該地區的名稱不可用我害怕。

+0

我試過本地主機,它的顯示:'+00:00',可能在'localhost'上不行' – Frank