Timezone control in php using ics date / time format
Ok, I am using the ICS analyzer utility to parse the Google calendar ICS files. It works great, except that Google feeds me the time of events in UCT .. so I need to subtract 5 hours and 6 hours when daylight saving time occurs.
To get the start time, I use:
$timestart = date("g:iA",strtotime(substr($event['DTSTART'], 9, -3)));
//$event['DTSTART'] feeds me back the date in ICS format: 20100406T200000Z
So, any guidelines for managing time zone and daylight savings time?
Thank you in advance
source to share
Just don't use the substr () part of the code. strtotime can parse yyyymmddThhiissZ
a formatted string and interpret Z as timezone = utc.
eg.
$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
change or use 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";
(the output is the same)
source to share
If you have set your timezone locale, you can also use a date ("T"), which will return the current timezone.
echo date("T");
Since we are in daylight saving time (in my timezone) it returns: EDT
You can then use this in an IF statement to adjust your timezone setting.
if (date("T") == 'EDT')
$adjust = 6;
else
$adjust = 5;