Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing timezone in php with ics date/time format

Ok, I'm using an ICS parser utility to parse google calendar ICS files. It works great, except google is feeding me the times of the events at UCT.. so I need to subtract 5 hours now, and 6 hours when daylight savings happens.

To get the start time I'm using:

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

So any suggestions how to handle timezone and daylight savings time?

Thanks in advance

like image 976
John Avatar asked Apr 13 '26 00:04

John


1 Answers

Just don't use the substr() part of the code. strtotime is able to parse the yyyymmddThhiissZ formatted string and interprets the Z as timezone=utc.

e.g.

$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";

prints

Tue, 06 Apr 2010 22:00:00 +0200
Tue, 06 Apr 2010 16:00:00 -0400

edit: or use the DateTime and DateTimezone classes

$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";

(output is the same)

like image 117
VolkerK Avatar answered Apr 17 '26 02:04

VolkerK