tags:

views:

442

answers:

2

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

+1  A: 

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)

VolkerK
using date(DateTime::RFC1123, $ts) it prints: Fri, 21 Aug 1970 10:26:52 -0500 for "20100406T200000Z"
John
In fact for all dates using "20100406T200000Z" format, it is outputting Fri, 21 Aug 1970 with the only thing that is changing is the time. That is why I went to the substr method, so I could get the right time. Is it a problem with my php ini file? This is running on Windows Server 2003.
John
I've tested it with php 5.3.2/winxp. Which version do you use?
VolkerK
Windows NT EQ-610 5.2 build 3790
John
I think I've found where the problem is coming up. I'll update here when I get it fixed.
John
Ok, I had something else messing with the $timestart string and that was what gave me the aug 1970 output. Thanks for your help VolkerK.
John
A: 

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;
nageeb