calendar/icsfile

This simple class generate a .ics file.

9.2.2 2025-08-10 15:14 UTC

README

This library allows you to easily generate iCalendar (.ics) files in PHP, following the iCalendar (RFC 5545) specification.

Installation

Make sure you have installed the dependencies via Composer:

composer install

Include the Composer autoloader in your script:

require_once 'vendor/autoload.php';

Usage

Below is a basic example of creating an iCalendar event:

<?php

require_once 'vendor/autoload.php';

use Ical\Ical;
use Ical\IcalendarException;

try {
    $ical = (new Ical())
        ->setName('test')
        ->setAddress('Paris')
        ->setDateStart(new \DateTimeImmutable('2014-11-21 15:00:00'))
        ->setDateEnd(new \DateTimeImmutable('2014-11-21 16:00:00'))
        ->setDateStamp(new \DateTimeImmutable('2014-11-21 15:00:00'))
        ->setCalendarType(CalendarTypeEnum::GREGORIAN)
        ->setTransparency(TransparencyEnum::OPAQUE) // Optional
        ->setDescription('wonder description')
        ->setSummary('Running')
        ->setOrganizer('foo@bar.fr') // Optional
        ->setFilename('myFileName')
        ->setStatus('CONFIRMED') // Optional
        ->setSequence(2); // Number of updates (default is 1, optional)

    $ical->addHeader();

    echo $ical->getICAL();

} catch (IcalendarException $exc) {
    echo $exc->getMessage();
}

Methods

setName(string $name)

Sets the event name.

setAddress(string $address)

Sets the event location.

setDateStart(\DateTimeImmutable $date)

Sets the start date and time of the event.

setDateEnd(\DateTimeImmutable $date)

Sets the end date and time of the event.

setDateStamp(\DateTimeImmutable $date)

Sets the creation date/time of the event.

setCalendarType(CalendarTypeEnum $type)

Sets the calendar type (e.g., GREGORIAN).

setTransparency(TransparencyEnum $transparency)

Sets whether the event is opaque or transparent for scheduling (optional).

setDescription(string $description)

Sets the event description.

setSummary(string $summary)

Sets the short summary/title of the event.

setOrganizer(string $email)

Sets the organizer’s email address (optional).

setFilename(string $filename)

Sets the filename for the generated .ics file (without extension).

setStatus(string $status)

Sets the event status (CONFIRMED, TENTATIVE, etc.) (optional).

setSequence(int $sequence)

Sets the sequence number for event updates (default is 1, optional).

addHeader()

Adds the required iCalendar headers.

getICAL(): string

Returns the generated iCalendar content.

Example Output

GIN:VCALENDAR
VERSION:2.0
PRODID:test
TRANSP:OPAQUE
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTART:20141121T150000Z
DTEND:20141121T160000Z
DTSTAMP:20141121T150000Z
SUMMARY:Running
UID:myUid
ORGANIZER:MAILTO:foo@bar.fr
LOCATION:Paris
DESCRIPTION:wonder description
SEQUENCE:2
STATUS:CONFIRMED
END:VEVENT
END:VCALENDAR

Error Handling

All exceptions are instances of IcalendarException.
Use try/catch to handle errors when generating the calendar.