Back to integration guides

July 10, 2026

How to Add an Event to Apple Calendar from a PHP Web App

A developer guide to generating ICS files in PHP to allow users to add events to Apple Calendar.

Generating Apple Calendar Events in PHP

If you're building a custom event platform in PHP, you'll inevitably need to let users save events to Apple Calendar (or Outlook).

Both of these platforms rely on the .ics file format.

The Code

To generate an ICS file dynamically in PHP, you need to set the correct HTTP headers and output the iCalendar payload.

<?php
// Set correct headers for ICS download
header('Content-Type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename="event.ics"');
 
// Define event details
$start = gmdate('Ymd\THis\Z', strtotime('2026-08-01 15:00:00'));
$end = gmdate('Ymd\THis\Z', strtotime('2026-08-01 16:00:00'));
$summary = "Developer Meetup";
$description = "Monthly developer sync.";
$location = "San Francisco, CA";
 
// Output the payload
echo "BEGIN:VCALENDAR\r\n";
echo "VERSION:2.0\r\n";
echo "PRODID:-//My App//EN\r\n";
echo "BEGIN:VEVENT\r\n";
echo "UID:" . uniqid() . "\r\n";
echo "DTSTAMP:" . gmdate('Ymd\THis\Z') . "\r\n";
echo "DTSTART:{$start}\r\n";
echo "DTEND:{$end}\r\n";
echo "SUMMARY:{$summary}\r\n";
echo "DESCRIPTION:{$description}\r\n";
echo "LOCATION:{$location}\r\n";
echo "END:VEVENT\r\n";
echo "END:VCALENDAR\r\n";
?>

The Better Way

Formatting dates, handling timezones, escaping special characters, and dealing with line-folding rules in RFC 5545 is notoriously difficult to get right across every single calendar app version.

Instead of maintaining this code yourself, you can use Invitly. Just create the event in the dashboard and embed our universal calendar button into your PHP templates.