使用ics日期/时间格式管理php中的时区

Joh*_*ohn 3 php time icalendar date

好的,我正在使用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
Run Code Online (Sandbox Code Playgroud)

那么如何处理时区和夏令时的任何建议?

提前致谢

Vol*_*erK 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";
Run Code Online (Sandbox Code Playgroud)

版画

Tue, 06 Apr 2010 22:00:00 +0200
Tue, 06 Apr 2010 16:00:00 -0400
Run Code Online (Sandbox Code Playgroud)

编辑:或使用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";
Run Code Online (Sandbox Code Playgroud)

(输出相同)