In current days I’m working on new version of web site for HaDivadlo theater and I wanted to render their program also as an ICalendar file. I didn’t used any external framework only my PHP implementation of Google CTemplate and MazaarPHP – simple PHP 5 framework especially aimed on web pages.

Here is the truncated code of PHP file:

<?php
require_once 'inc/mazaarphp_simple/mazaarphpsimple.inc.php';

// ...
$cal_dict = new TemplateDictionary('icalendar');
$stmt = $db->prepareQueryFromString($query, array($year.'-'.$month));
if($stmt instanceof DbStatement) {
  $res = $stmt->execute();
  if($res instanceof DBResult) {
    for($i=0; $i<$res->rows; $i++) {
      $event_dict = $cal_dict->AddSectionDictionary('VCALENDAR_EVENT');
      $date_vals = parse_db_date($res->content($i, 2));
      $event_dict->SetValue('YEAR', $date_vals[1]);
      $event_dict->SetValue('MONTH', $date_vals[2]);
      $event_dict->SetValue('DAY', $date_vals[3]);
      $event_dict->SetValue('HOUR', 19);
      $event_dict->SetValue('MINUTES', 30);
      $event_dict->SetValue('SECONDS', 0);
      $event_dict->SetValue('HOUR_DUR', 21);
      $event_dict->SetValue('TITLE', getItemValue('inscenace', 'title', $res->content($i, 1)));
    }
  }
}

// Print calendar file
header('Content-Type: text/calendar;charset=UTF-8 '.EOL);
header('Content-Disposition: attachment; filename="hadivadlo.ics"'.EOL);
$cal_tpl = new Template('inc/templates/icalendar.ics.tpl', DO_NOT_STRIP);
echo $cal_tpl->Expand($cal_dict);
?>

And here of template file:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
{{#VCALENDAR_EVENT}}
BEGIN:VEVENT
DTSTART:{{YEAR}}{{MONTH}}{{DAY}}T{{HOUR}}{{MINUTES}}{{SECONDS}}Z
DTEND:{{YEAR}}{{MONTH}}{{DAY}}T{{HOUR_DUR}}{{MINUTES}}{{SECONDS}}Z
SUMMARY:{{TITLE}}
END:VEVENT
{{/VCALENDAR_EVENT}}
END:VCALENDAR

And here is the result in commonly used calendars:

HaDivadlo’s program in Google Calendar HaDivadlo’s program in Mozilla Sunbird HaDivadlo’s program in Evolution HaDivadlo’s program in Microsoft Outlook 2007

The only one client (from all which I tested) didn’t successfully import my calendar – it was Microsoft Windows Calendar which is bundled with Windows Vista – I didn’t find out by which is this caused but still I’m enjoying this work :) (my first meeting with ICalendar files)…