2011-04-07 60 views
0

我在美國服務器上運行託管服務,該服務器讀取已使用本地日期創建的XML饋送 - 目前只是英國,但我希望確保該服務適用於所有時區。 我的流程會查看Feed中文章的日期,並將其與日期/時間(在美國的服務器上)進行比較。 我想出瞭解決局部化系統到飼料的鼻祖,然後創建與時間戳比較「現在」有:在PHP中處理時區和日期

protected function datemath($thedate){ 

    $currenttimezone = date_default_timezone_get(); 
    date_default_timezone_set($this->feedtimezone); 
    $thedate = mktime substr($thedate,11,2),substr($thedate,14,2), 
    substr($thedate,17,2),substr($thedate,3,2),substr($thedate,0,2), 
    substr($thedate,6,4)); 
    date_default_timezone_set($currenttimezone); 
    return $thedate; 

    } 

我的問題是這...這是一個合理的方式處理這個問題還是有一個更好,更標準化的方式,我真的應該知道?

+0

相關問題:http://stackoverflow.com/q/2532729/1583 – Oded 2011-04-07 19:44:49

+0

您正在使用mktime來中斷您從feed和date_default_timezone_set得到的時間,以確保您獲得關於提供時區?是的,很合理... – Khez 2011-04-07 19:45:35

+0

@Oden哇 - 這是一個非常全面的答案。很明顯,它和我發現的一樣痛苦。 – Stevo 2011-04-07 19:49:15

回答

0

的其他民族的代碼,我看到的功能

strtotime($thedate); 

比使用mktime更簡潔一點,也多一點檢查後允許不同的時間格式。

1

這是我寫的功能來做時區轉換。應該是不言自明:

function switch_timezone($format, $time = null, 
    $to = "America/Los_Angeles", $from = "America/Los_Angeles") 
{ 
    if ($time == null) $time = time(); 

    $from_tz = new DateTimeZone($from); 
    $to_tz = new DateTimeZone($to); 

    if (is_int($time)) $time = '@' . $time; 

    $dt = date_create($time, $from_tz); 

    if ($dt) 
    { 
     $dt->setTimezone($to_tz); 
     return $dt->format($format); 
    } 

    return date($format, $time); 
} 
+1

在將它傳遞給DateTime構造函數/ date_create時,您實際上可以在unix時間戳的前面粘貼'@'](http://us2.php.net/manual/en/datetime.formats.compound.php)。無需跳過格式化箍。 – Charles 2011-04-07 19:46:46

+0

好的小費,謝謝! – 2011-04-07 19:48:12

+0

啊,不,我的意思是如果(is_int($ time))$ time ='@'。 $ time;' – Charles 2011-04-07 19:53:22

相關問題