Generating iCalendar (.ics) files and subscribe feeds
icalendaricsrfc5545calendartimezonesrecurrencephp
An .ics file (iCalendar, RFC 5545) is how a web page hands an event to Google Calendar, Apple
Calendar or Android's calendar apps. The same format served at a stable URL is a subscribe
feed: the calendar app re-downloads it periodically and shows the events read-only. The format
is plain text but strict, and calendar apps silently drop events that break the rules. The
notes below come from the cal.jhampton.io web app (PHP + SQLite), which serves a feed per shared
calendar (/s/<viewKey>.ics) and one file per shared event (/e/<shareKey>.ics).
Skeleton
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//example.com//Calendar//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Team calendar (feeds only: display name)
REFRESH-INTERVAL;VALUE=DURATION:PT1H (feeds only: refresh hint)
X-PUBLISHED-TTL:PT1H (same hint, older clients)
BEGIN:VTIMEZONE ... END:VTIMEZONE (one per TZID used)
BEGIN:VEVENT
UID:<stable-id>@example.com
DTSTAMP:20261027T150000Z
DTSTART:20261101T180000Z
DTEND:20261101T190000Z
SUMMARY:Dinner
END:VEVENT
END:VCALENDAR
Serve it as Content-Type: text/calendar; charset=utf-8 with
Content-Disposition: inline; filename="name.ics" (sanitize the filename).
Rules that bite
- Line endings are CRLF, including the last line.
- Fold lines at 75 octets (bytes, not characters): insert CRLF plus one space and continue. Count UTF-8 bytes and never split a multibyte character.
- Escape text values: backslash first, then
;→\;,,→\,, newlines →\n. Strip other control characters on input. UIDmust be stable and globally unique. A feed that changes UIDs on each fetch makes clients duplicate or flicker events. Use the record's permanent key plus your domain.DTSTAMPis required; use the record's last-modified time in UTC.- All-day events use
DTSTART;VALUE=DATE:20261101and an exclusiveDTEND(the day after the last day). Store all-day ends exclusive too and the mapping is direct. - Give every event a
SUMMARY, even a placeholder like "(No title)"; some clients hide events without one. - A
URLproperty and a link at the end ofDESCRIPTIONtake the user back to the web page.
Time zones and recurring events
- A one-off timed event is simplest in UTC (
DTSTART:20261101T180000Z); every client converts to local time. - A recurring event must be written in its own zone (
DTSTART;TZID=America/Chicago:20261101T120000). "Every Monday at noon" in UTC drifts by an hour across a daylight-saving change. - Any
TZIDyou use needs a matchingVTIMEZONEblock. Generate it from the tz database rather than hand-writing it: in PHP,DateTimeZone::getTransitions($from, $to)gives each change with its offset, DST flag and abbreviation; emit aDAYLIGHTorSTANDARDsub-block per transition (DTSTARTin the old local time,TZOFFSETFROM,TZOFFSETTO,TZNAME) over a span of years starting just before the event. - Validate zone names against
DateTimeZone::listIdentifiers()and fall back to UTC. - Keep
RRULEsupport small and validated. The app accepts onlyFREQ=DAILY|WEEKLY|MONTHLY|YEARLYplus standard parameters, which covers almost every real calendar entry.
Feeds vs files
- An
.icsfile is a one-time import; later edits do not reach the user's calendar. - A feed URL is subscribed to. Google Calendar refreshes external feeds on its own schedule
(often 12 hours or more) and generally ignores
REFRESH-INTERVAL; Apple and most Android apps honor it more closely. Tell users that changes are not instant. - Use
Cache-Control: no-cacheso an intermediate cache does not hold an old version. - The feed URL is a read capability: anyone with it can read the calendar, so use an unguessable key and allow regenerating it. See "Anyone with the link" share links (capability URLs).