Wrong time (timezone) when creating ics file with PHP

Viewed 1565

I'm creating an ics file with PHP like this:

<?php
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=event.ics');
?>

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//My company//My name//DE
METHOD:PUBLISH
BEGIN:VEVENT
UID:<?php echo base64_encode(random_bytes(64))."\n";?>
LOCATION:At my house
SUMMARY:Meeting
DESCRIPTION:Important meeting
CLASS:PUBLIC
DTSTART:20210201T100000Z
DTEND:20210201T150000Z
END:VEVENT
END:VCALENDAR

The event starts at 10 o'clock and ends at 15 o'clock German time, as specified in the ics file. I'm located in Germany and I want to use the German time zone.

After downloading, I imported this in the Calendar app on my Mac. Unfortunately, the event gets created with starting at 11 o'clock and ending at 16 o'clock, instead of 10 o'clock and 15 o'clock, as specified in the ics file.

I think there's something wrong with the time zone. How can I set the right zone? What's the proper way to so that?

3 Answers

The best way to configure timezone on PHP is to set it in configuration file

   TZID:Europe/London

Just change it accordingly to match your timezone.

Your times are entered as UTC time and are displayed as local time after the download. If local times are given, you must convert them to UTC time for the ICS entry.. You can use gmdate() for this, for example.

The RFC5545 specification will help. It is useful to get familiar with it. https://www.rfc-editor.org/rfc/rfc5545#section-3.3.5 you will see there are 3 ways to specify a date-time, each way will have a different effect on how the date time is dealt with by the receiving application. Choose the way that makes the most sense for the events you are generating.

There are also many posts here on stack that may make it clearer if you find the spec hard to understand:

and so on

When testing future dates, consider that daylight saving of both the event timezone and the receiving calendar may also affect the time displayed.

Related