A Composer package for creating icalendar files (ZContent/icalendar)
(https://github.com/zcontent/icalendar), (*1)
The Zap Calendar iCalendar Library is a PHP library for supporting the iCalendar (RFC 5545) standard., (*2)
This PHP library is for reading and writing iCalendar formatted feeds and files. Features of the library include:, (*3)
All iCalendar data is stored in a PHP object tree. This allows any property to be added to the iCalendar feed without requiring specialized library function calls. With power comes responsibility. Missing or invalid properties can cause the resulting iCalendar file to be invalid. Visit iCalendar.org to view valid properties and test your feed using the site's iCalendar validator tool., (*4)
Library API documentation can be found at http://icalendar.org/zapcallibdocs, (*5)
See the examples folder for programs that read and write iCalendar files. At its simpliest, you need to include the library at the top of your program:, (*6)
require_once($path_to_library . "/zapcallib.php");
Create an ical object using the ZCiCal object:, (*7)
$icalobj = new ZCiCal();
Add an event object:, (*8)
$eventobj = new ZCiCalNode("VEVENT", $icalobj->curnode);
Add a start and end date to the event:, (*9)
// add start date $eventobj->addNode(new ZCiCalDataNode("DTSTART:" . ZCiCal::fromSqlDateTime("2020-01-01 12:00:00"))); // add end date $eventobj->addNode(new ZCiCalDataNode("DTEND:" . ZCiCal::fromSqlDateTime("2020-01-01 13:00:00")));
Write the object in iCalendar format using the export() function call:, (*10)
echo $icalobj->export();
This example will not validate since it is missing some required elements. Look at the simpleevent.php example for the minimum # of elements needed for a validated iCalendar file., (*11)
To create a multi-event iCalendar file, simply create multiple event objects. For example:, (*12)
$icalobj = new ZCiCal(); $eventobj1 = new ZCiCalNode("VEVENT", $icalobj->curnode); $eventobj1->addNode(new ZCiCalDataNode("SUMMARY:Event 1")); ... $eventobj2 = new ZCiCalNode("VEVENT", $icalobj->curnode); $eventobj2->addNode(new ZCiCalDataNode("SUMMARY:Event 2")); ...
To read an existing iCalendar file/feed, create the ZCiCal object with a string representing the contents of the iCalendar file:, (*13)
$icalobj = new ZCiCal($icalstring);
Large iCalendar files can be read in chunks to reduce the amount of memory needed to hold the iCalendar feed in memory. This example reads 500 events at a time:, (*14)
$icalobj = null; $eventcount = 0; $maxevents = 500; do { $icalobj = newZCiCal($icalstring, $maxevents, $eventcount); ... $eventcount +=$maxevents; } while($icalobj->countEvents() >= $eventcount);
You can read the events from an imported (or created) iCalendar object in this manner:, (*15)
foreach($icalobj->tree->child as $node) { if($node->getName() == "VEVENT") { foreach($node->data as $key => $value) { if($key == "SUMMARY") { echo "event title: " . $value->getValues() . "\n"; } } } }