2011-04-07 73 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