只要不使用代码的SUBSTR()的一部分。 strtotime能够解析格式化为yyyymmddThhiissZ
的字符串,并将Z解释为timezone = utc。
例如
$event = array('DTSTART'=>'20100406T200000Z');
$ts = strtotime($event['DTSTART']);
date_default_timezone_set('Europe/Berlin');
echo date(DateTime::RFC1123, $ts), "\n";
date_default_timezone_set('America/New_York');
echo date(DateTime::RFC1123, $ts), "\n";
打印
Tue, 06 Apr 2010 22:00:00 +0200
Tue, 06 Apr 2010 16:00:00 -0400
编辑:或使用DateTime和DateTimezone类
$event = array('DTSTART'=>'20100406T200000Z');
$dt = new DateTime($event['DTSTART']);
$dt->setTimeZone(new DateTimezone('Europe/Berlin'));
echo $dt->format(DateTime::RFC1123), "\n";
$dt->setTimeZone(new DateTimezone('America/New_York'));
echo $dt->format(DateTime::RFC1123), "\n";
(输出是一样的)
使用日期(日期时间:: RFC1123,$ TS)它打印:周五,1970年8月21日10时26分52秒-0500对于 “20100406T200000Z” – John 2010-04-09 17:04:00
事实上,对于使用 “20100406T200000Z” 格式的所有日期,它是输出1970年8月21日星期五的唯一变化就是时间。这就是为什么我去了substr方法,所以我可以得到正确的时间。这是我的php ini文件的问题吗?这在Windows Server 2003上运行。 – John 2010-04-09 17:15:01
我已经用php 5.3.2/winxp测试过它。你使用哪个版本? – VolkerK 2010-04-09 17:23:28