Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
I

icalendar-generator

> 后端框架
Open source

Generate calendars in the iCalendar format

689 stars0 likes2 views
WebsiteGitHub

About

Generate calendars in the iCalendar format

Generate calendars in the iCalendar format

Want to create online calendars so that you can display them on an iPhone's calendar app or in Google Calendar? This can be done by generating calendars in the iCalendar format (RFC 5545), a textual format that can be loaded by different applications.

The format of such calendars is defined in RFC 5545, which is not a pleasant reading experience. This package implements RFC 5545 and some extensions from RFC 7986 to provide you an easy to use API for creating calendars. It's not our intention to implement these RFC's entirely but to provide a straightforward API that's easy to use.

Here's an example of how to use it:

php
use Spatie\IcalendarGenerator\Components\Calendar;
use Spatie\IcalendarGenerator\Components\Event;

Calendar::create('Laracon online')
    ->event(Event::create('Creating calender feeds')
        ->startsAt(new DateTime('6 March 2019 15:00'))
        ->endsAt(new DateTime('6 March 2019 16:00'))
    )
    ->get();

The above code will generate this string:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:spatie/icalendar-generator
NAME:Laracon online
X-WR-CALNAME:Laracon online
BEGIN:VEVENT
UID:5ef5c3f64cb2c
DTSTAMP;TZID=UTC:20200626T094630
SUMMARY:Creating calendar feeds
DTSTART:20190306T150000Z
DTEND:20190306T160000Z
DTSTAMP:20190419T135034Z
END:VEVENT
END:VCALENDAR

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

bash
composer require spatie/icalendar-generator

Upgrading

There were some substantial changes between v1 and v2 of the package. Check the upgrade guide for more information.

Usage

Here's how you can create a calendar:

php
$calendar = Calendar::create();

You can give a name to the calendar:

php
$calendar = Calendar::create('Laracon Online');

A description can be added to a calendar:

php
$calendar = Calendar::create()
    ->name('Laracon Online')
    ->description('Experience Laracon all around the world');

In the end, you want to convert your calendar to text so that it can be streamed or downloaded to the user. Here's how you do that:

php
Calendar::create('Laracon Online')->get(); // BEGIN:VCALENDAR ...

When streaming a calendar to an application, it is possible to set the calendar's refresh interval by duration in minutes. When setting this, the calendar application will check your server every time after the specified duration for changes to the calendar:

php
Calendar::create('Laracon Online')
    ->refreshInterval(5)
    ...

Event

An event can be created as follows. A name is not required, but a start date should always be given:

php
Event::create('Laracon Online')
    ->startsAt(new DateTime('6 march 2019'));

You can set the following properties on an event:

php
Event::create()
    ->name('Laracon Online')
    ->description('Experience Laracon all around the world')
    ->uniqueIdentifier('A unique identifier can be set here')
    ->createdAt(new DateTime('6 march 2019'))
    ->startsAt(new DateTime('6 march 2019 15:00'))
    ->endsAt(new DateTime('6 march 2019 16:00'));

Want to create an event quickly with a start and end date?

php
Event::create('Laracon Online')
    ->period(new DateTime('6 march 2019'), new DateTime('7 march 2019'));

You can add a location to an event a such:

php
Event::create()
    ->address('Kruikstraat 22, 2018 Antwerp, Belgium')
    ->addressName('Spatie HQ')
    ->coordinates(51.2343, 4.4287)
    ...

You can set the organizer of an event, the email address is required, but the name can be omitted:

php
Event::create()
    ->organizer('[email protected]', 'Ruben')
    ...

Attendees of an event can be added as such:

php
Event::create()
    ->attendee('[email protected]') // only an email address is required
    ->attendee('[email protected]', 'Brent')
    ...

You can also set the participation status of an attendee:

php
Event::create()
    ->attendee('[email protected]', 'Ruben', ParticipationStatus::Accepted)
    ...

There are five participation statuses:

  • ParticipationStatus::Accepted
  • ParticipationStatus::Declined
  • ParticipationStatus::Tentative
  • ParticipationStatus::NeedsAction
  • ParticipationStatus::Delegated

You can indicate that an attendee is required to RSVP to an event:

php
Event::create()
    ->attendee('[email protected]', 'Ruben', ParticipationStatus::NeedsAction, requiresResponse: true)
    ...

An event can be made transparent, so it does not overlap visually with other events in a calendar:

php
Event::create()
    ->transparent()
    ...

It is possible to create an event that spans a full day:

php
Event::create()
    ->fullDay()
    ->startsAt(new DateTime('6 March 2019'))
    ->endsAt(new DateTime('7 March 2019'))

The iCalendar spec defines the end of an event as non inclusive, so the end date is the day after the last day of the event. The event above takes place on March 6th only. Your calendar application will display it on March 6th.

This package writes the end date exactly as you provide it, it never adds a day for you. A three day event running from March 6th up to and including March 8th therefore ends on March 9th:

php
Event::create()
    ->fullDay()
    ->startsAt(new DateTime('6 March 2019'))
    ->endsAt(new DateTime('9 March 2019'))

For an event that spans a single day you can leave out the end date. The spec defines a full day event without an end date as lasting exactly one day:

php
Event::create()
    ->fullDay()
    ->startsAt(new DateTime('6 March 2019'))

Do not pass the same date as start and end. The spec requires the end to be later than the start, so startsAt(new DateTime('6 March 2019')) combined with endsAt(new DateTime('6 March 2019')) produces a file that calendar applications may reject or render as an empty event.

The status of an event can be set:

php
Event::create()
    ->status(EventStatus::Cancelled)
    ...

There are three event statuses:

  • EventStatus::Confirmed
  • EventStatus::Cancelled
  • EventStatus::Tentative

An event can be classified(Public, Private, Confidential) as such:

php
Event::create()
    ->classification(Classification::Private)
    ...

You can add a url attachment as such:

php
Event::create()
    ->attachment('https://spatie.be/logo.svg')
    ->attachment('https://spatie.be/feed.xml', 'application/json')
    ...

You can add an embedded attachment (base64) as such:

php
Event::create()
    ->embeddedAttachment($file->toString())
    ->embeddedAttachment($fileString, 'application/json')
    ->embeddedAttachment($base64String, 'application/json', needsEncoding: false)
    ...

You can add an image as such:

php
Event::create()
    ->image('https://spatie.be/logo.svg')
    ->image('https://spatie.be/logo.svg', 'text/svg+xml')
    ->image('https://spatie.be/logo.svg', 'text/svg+xml', Display::Badge)
    ...

There are four different image display types:

  • Display::Badge
  • Display::Graphic
  • Display::Fullsize
  • Display::Thumbnail

You can add a sequence to an event as such:

php
Event::create()
    ->sequence(1)
    ...

After creating your event, it should be added to a calendar. There are multiple options to do this:

php
// As a single event parameter
$event = Event::create('Creating calendar feeds');

Calendar::create('Laracon Online')
    ->event($event)
    ...

// As an array of events
Calendar::create('Laracon Online')
    ->event([
        Event::create('Creating calender feeds'),
        Event::create('Creating contact lists'),
    ])
    ...

// As a closure
Calendar::create('Laracon Online')
    ->event(function(Event $event){
        $event->name('Creating calender feeds');
    })
    ...

Using Carbon

You can use the popular Carbon library:

php
use Carbon\Carbon;

Event::create('Laracon Online')
    ->startsAt(Carbon::now())
    ...

Timezones

Events will use the timezones defined in the DateTime objects you provide. PHP always sets these timezones in a DateTime object. By default, this will be the UTC timezone, but it is possible to change this.

Just a reminder: do not use PHP's setTimezone function on a DateTime object, it will change the time according to the timezone! It is better to create a new DateTime object with a timezone as such:

php
new DateTime('6 march 2019 15:00', new DateTimeZone('Europe/Brussels'))

A point can be made for omitting timezones. For example, when you want to show an event at noon in the world. We define noon at 12 o'clock, but that time is relative. It is not the same for people in Belgium, Australia, or any other country in the world.

That's why you can disable timezones on events:

php
$starts = new DateTime('6 march 2019 12:00')

Event::create()
    ->startsAt($starts)
    ->withoutTimezone()
    ...

You can even disable timezones for a whole calendar:

php
Calendar::create()
   ->withoutTimezone()
    ...

Each calendar should have Timezone components describing the timezones used within your calendar. Although not all calendar clients require this, it is recommended to add these components.

Creating such Timezone components is quite complicated. That's why this package will automatically add them for you without configuration.

You can disable this behaviour as such:

php
Calendar::create()
   ->withoutAutoTimezoneComponents()
    ...

Quick note, when using UTC offsets as timezones(e.g. +02:00), no Timezone components will be added automatically and the dates will automatically be converted to UTC.

php
$starts = new DateTime('6 march 2019 12:00', new DateTimeZone('+02:00'))

Event::create()->startsAt($starts); // DTSTART will be 20190306T100000Z

You can manually add timezones to a calendar if desired as such:

php
$timezoneEntry = TimezoneEntry::create(
    TimezoneEntryType::Daylight,
    new DateTime('23 march 2020'),
    '+00:00',
    '+02:00'
);

$timezone = Timezone::create('Europe/Brussels')
    ->entry($timezoneEntry)
    ...

Calendar::create()
    ->timezone($timezone)
    ...

More on these timezones later.

Alerts

Alerts allow calendar clients to send reminders about specific events. For example, Apple Mail on an iPhone will send users a notification about the event. An alert always belongs to an event has a description and a number of minutes before the event it will be triggered:

php
Event::create('Laracon Online')
    ->alertMinutesBefore(5, 'Laracon online is going to start in five minutes');

You can also trigger an alert after the event:

php
Event::create('Laracon Online')
    ->alertMinutesAfter(5, 'Laracon online has ended, see you next year!');

Or trigger an alert on a specific date:

php
Event::create('Laracon Online')
    ->alertAt(
       new DateTime('05/16/2020 12:00:00'),
       'Laracon online has ended, see you next year!'
    );

Removing timezones on a calendar or event will also r

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPcalendarhacktoberfesticalendarics

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架