user1033882
user1033882

Reputation: 123

.ics export from Google calendar has wrong time

Using PHP,mysql, i am trying to export/import calendar data from/to Google calendar. The importing (to Google) works fine. I can export data from my mysql table and create an ics file then import it into Google Calendar, using a specific timezone.

The problem is when i am exporting the data... Google changes the timezone for every event. I'll give an example.

When i am importing the data to Google Calender, a typical event looks like this:

BEGIN:VEVENT
UID:20
DTSTAMP:20160328T222128
DTSTART:20160328T033000
DTEND:20160328T043000
LOCATION:
DESCRIPTION:My description here
SUMMARY:Event number 3
END:VEVENT

The StartTime is 033000 and EndTime is 043000. Everything is fine, i see an event starting at 3:30am and ending at 4:30am.

When i am exporting my calendar from Google this event looks like this:

BEGIN:VEVENT
DTSTART:20160328T003000Z
DTEND:20160328T013000Z
DTSTAMP:20160328T194501Z
UID:20
CREATED:20160328T224357Z
DESCRIPTION:My description here
LAST-MODIFIED:20160328T194410Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Event number 3
TRANSP:OPAQUE
END:VEVENT

StartTime 003000 EndTime 013000

Both .ics files have X-WR-TIMEZONE:Europe/Athens

Why on earth is Google sending me back Zulu time, instead of what i am seeing in the calendar ? I dont want to handle timezones. This means if a person from the Usa uploads an .ics file to the custom calendar i am making, it will insert wrong times in the database. Is it possible, Google Calendar to export exactly the hours a person sees in his calendar?

If i cant avoid this, what is the easiest way to convert this Z time into the proper time ? (the time that person sees in Google Calendar)

Thanks!

Upvotes: 0

Views: 1631

Answers (1)

Evert
Evert

Reputation: 99505

I assume that, since you are able to insert the date/times into a database, you are able to parse the format of DTSTART. If not, I recommend taking a look at sabre/vobject for a full-on iCalendar parser.

So after parsing DTSTART, make sure you parse this into a php DateTime object. This object makes it really easy to convert to different timezones:

$dt = new \DateTime($input);
$dt->setTimeZone(new DateTimeZone('Europe/Athens'));

After that, your $dt contains your start time, converted to your timezone.

Here's an example with vobject:

$cal = Sabre\VObject\Reader::read($iCalendarData);

// DTSTART property
$dtstart = $cal->VEVENT->DTSTART;

// Turn into DateTime object
$dateTime = $dtstart->getDateTime();

// Convert to your timezone
$newDateTime = $dateTime->setTimeZone('Europe/Athens');

Disclaimer: I'm the main author for vobject.

Upvotes: 3

Related Questions