3

好的,我正在使用 ICS 解析器实用程序来解析谷歌日历 ICS 文件。它工作得很好,除了谷歌正在给我提供 UCT 的事件时间。所以我现在需要减去 5 小时,而夏令时发生时需要减去 6 小时。

要获得我正在使用的开始时间:

$timestart = date("g:iA",strtotime(substr($event['DTSTART'], 9, -3)));
//$event['DTSTART'] feeds me back the date in ICS format: 20100406T200000Z

那么有什么建议如何处理时区和夏令时吗?

提前致谢

4

2 回答 2

8

只是不要使用代码的 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";

(输出相同)

于 2010-04-09T15:28:07.583 回答
0

如果您设置了时区语言环境,您还可以使用 date("T") 来返回当前时区。

echo date("T");

因为我们处于夏令时,(在我的时区)它返回:EDT

然后你可以在 IF 语句中使用它来调整你的时区调整。

if (date("T") == 'EDT')
  $adjust = 6;
else
  $adjust = 5;
于 2010-04-09T15:32:27.250 回答