John
John

Reputation: 33

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

Upvotes: 3

Views: 7200

Answers (2)

VolkerK
VolkerK

Reputation: 96169

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)

Upvotes: 9

user312220
user312220

Reputation:

If you've set your timezone locale, you can also then use date("T") which will return the current timezone.

echo date("T");

Because we're in Daylight Saving Time, (in my timezone) it returns: EDT

Then you could use that in an IF statement to adjust your timezone adjustment.

if (date("T") == 'EDT')
  $adjust = 6;
else
  $adjust = 5;

Upvotes: 0

Related Questions