DEV Community

Anton Dachauer
Anton Dachauer

Posted on

Generate .ics Files in PHP with Ease

If you’ve ever had to create .ics files in PHP, you probably know how tedious and error-prone it can be to handle the iCalendar format manually. That’s where opencal/ical comes in — a clean, modern PHP library that abstracts away the complexity of generating RFC 5545-compliant iCalendar files.

This package is a maintained fork of markuspoerschke/iCal and continues development with support for the latest PHP versions (8.1–8.4). It's already used as part of the OpenCal scheduling system but is fully standalone and easy to integrate into any project.

Why use this?

  • ✨ Clean, object-oriented API
  • 🧱 Strong separation between domain logic and rendering
  • 🧪 Fully tested and actively maintained
  • 📦 Composer installable

Installation

composer require opencal/ical 
Enter fullscreen mode Exit fullscreen mode

Basic usage

Here’s a minimal example that creates a single-day calendar event and writes it to a .ics file:

<?php require_once __DIR__ . '/vendor/autoload.php'; use OpenCal\iCal\Domain\Entity\Event; use OpenCal\iCal\Domain\Entity\Calendar; use OpenCal\iCal\Domain\ValueObject\SingleDay; use OpenCal\iCal\Domain\ValueObject\Date; use OpenCal\iCal\Presentation\Factory\CalendarFactory; // Create the event $event = (new Event()) ->setSummary('Christmas Eve') ->setDescription('Family dinner and gift exchange') ->setOccurrence( new SingleDay( new Date( new DateTimeImmutable('2030-12-24') ) ) ); // Create the calendar $calendar = new Calendar([$event]); // Generate the iCalendar component $calendarComponent = (new CalendarFactory())->createCalendar($calendar); // Save it to a file file_put_contents('christmas.ics', (string) $calendarComponent); // Or serve it via HTTP /* header('Content-Type: text/calendar; charset=utf-8'); header('Content-Disposition: attachment; filename="christmas.ics"'); echo $calendarComponent; */ 
Enter fullscreen mode Exit fullscreen mode

This will generate a valid .ics file that you can import into any calendar app — Google Calendar, Apple Calendar, Outlook, etc.

Project structure

The package is split into two main namespaces:

  • OpenCal\iCal\Domain\*: Domain models like Event, Calendar, Date, TimeSpan, etc.
  • OpenCal\iCal\Presentation\*: Responsible for converting the domain models into .ics strings

This makes it easy to test and extend the logic separately from the output format.

Looking ahead

We're actively maintaining the project on our GitLab instance, including adding more test coverage and documentation. Feel free to open issues or contribute — no corporate hoops, just a free account needed to get started.

Final thoughts

If you’re working with event data in PHP and need to export it to .ics files, opencal/ical will save you a ton of time. It’s small, focused, and does one thing well: generating calendar files that just work.

Check it out and let us know what you think. PRs and feedback always welcome!

Top comments (0)