使用ics日期/时间格式在php中管理时区
问题描述:
好吧,我正在使用ICS解析器实用程序来解析Google日历ICS文件。它的效果很好,除了谷歌给我UCT事件的时间。所以我需要现在减去5小时,以及夏令时6小时。使用ics日期/时间格式在php中管理时区
为了让我使用的开始时间:
$timestart = date("g:iA",strtotime(substr($event['DTSTART'], 9, -3)));
//$event['DTSTART'] feeds me back the date in ICS format: 20100406T200000Z
因此,任何建议如何处理时区和夏令时?
在此先感谢
答
只要不使用代码的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";
(输出是一样的)
答
如果你设置你的时区的区域设置,您也可以使用返回当前时区的日期(“T”)。
echo date("T");
因为我们是在夏令时,(在我的时区)返回: EDT
然后,你可以使用,在IF语句调整时区调整。
if (date("T") == 'EDT')
$adjust = 6;
else
$adjust = 5;
使用日期(日期时间:: 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