开发者

Create iCal Calendar event with PHP

I am trying to create a PHP script that wi开发者_JAVA技巧ll 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?


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:

  • The project page
  • The one and only usage example


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜