7
votes

I am trying to create a PHP script that will create a calendar event in iCal. I have searched here and in Google and only come up with results that talk about importing iCal events to a PHP-made calendar. This is the opposite of what I need.

I don't have any code to include because I have no starting point. Any suggestions on where I should start?

3
Do you mean iCal, the Apple calendar application, or iCalendar, the file format? (Though to solve the former will probably involve the latter, anyway...) - Matt Gibson
Thank you for the question of clarification. I need to create an iCal (Apple calendar) event. - Shattuck
Found this lib on github: github.com/markuspoerschke/iCal - Paolo Fulgoni

3 Answers

3
votes

A few years ago I had started writing an iCalendar library. It's in pretty alpha stage (and I 've practically given up on it), at the time there was no PHP 5, and there isn't a lot of functionality in there, but:

  • I do have a lot of code that goes into modeling the iCalendar RFC (you might want to look into it)
  • It does have the capability to programmatically create events and spit out iCal format

Hope it helps:

3
votes

Try this (from https://gist.github.com/jakebellacera/635416)

<?
// 1. Set the correct headers for this file
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=' . $filename);

// 2. Define helper functions

// Converts a unix timestamp to an ics-friendly format
// NOTE: "Z" means that this timestamp is a UTC timestamp. If you need
// to set a locale, remove the "\Z" and modify DTEND, DTSTAMP and DTSTART
// with TZID properties (see RFC 5545 section 3.3.5 for info)
//
// Also note that we are using "H" instead of "g" because iCalendar's Time format
// requires 24-hour time (see RFC 5545 section 3.3.12 for info).
function dateToCal($timestamp) {
  return date('Ymd\THis\Z', $timestamp);
}

// Escapes a string of characters
function escapeString($string) {
  return preg_replace('/([\,;])/','\\\$1', $string);
}

// 3. Echo out the ics file's contents
?>
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTEND:<?= dateToCal($dateend) ?>
UID:<?= uniqid() ?>
DTSTAMP:<?= dateToCal(time()) ?>
LOCATION:<?= escapeString($address) ?>
DESCRIPTION:<?= escapeString($description) ?>
URL;VALUE=URI:<?= escapeString($uri) ?>
SUMMARY:<?= escapeString($summary) ?>
DTSTART:<?= dateToCal($datestart) ?>
END:VEVENT
END:VCALENDAR
2
votes

Start Here. This will give you the file format for an icalendar event. then you can use php to output a file like this with your custom data:

http://en.wikipedia.org/wiki/ICalendar

I've used this as a reference point for projects in the past.