I am adding a feature to my event calendar application to provide iCalendar (ics) file downloads for the events. I want to generate the VTIMEZONE
Component, but all I have is the PHP's Timezone value from date_default_timezone_get()
. Here's an example of a VTIMEZONE
Component for Eastern Time (US & Canada) that was generated by Outlook:
BEGIN:VTIMEZONE
TZID:Eastern Time (US & Canada)
BEGIN:STANDARD
DTSTART:16011104T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010311T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
END:DAYLIGHT
END:VTIMEZONE
This would behave like PHP's "America/New_York" time zone, but how would I automate the generation of it?
Best Answer
PHP's
DateTimezone
class works with the Olson timezone database and has some (limited) methods to access offsets, transitions and short names.According to RFC 5545, the RRULE property is optional and therefore we should be able to generate a valid
VTIMEZONE
definition with the built-in utilities. Following the RFC suggestion, the following function does exactly this:The above code example uses the Sabre VObject library to create the
VTIMEZONE
definition but could easily be re-written to produce plain string output.In addition to the timezone identifier it takes two unix timestamps as arguments to define the time range we need timezone information for. Then all the relevant transitions for the given time range are listed.
I successfully tested the generated output with iTip invitations sent to Outlook which otherwise cannot match the plain Olson timezone identifiers to the Microsoft system.