Skip to content

Instantly share code, notes, and snippets.

@tomkrush
Created February 13, 2013 19:27
Show Gist options
  • Save tomkrush/4947361 to your computer and use it in GitHub Desktop.
Save tomkrush/4947361 to your computer and use it in GitHub Desktop.
Calendar event class for outputting an ics event file.
class CalendarEvent
{
public $description;
public $start;
public $end;
public function data()
{
$format = 'Ymd\THis';
$lines = array();
$lines[] = 'BEGIN:VCALENDAR';
$lines[] = 'VERSION:2.0';
$lines[] = 'PRODID://Wordpress//EN';
$lines[] = 'BEGIN:VEVENT';
$lines[] = 'UID:'.md5($this->start . $this->end . $this->description . time());
$lines[] = 'DTSTART:'.date($format, $this->start);
$lines[] = 'DTEND:'.date($format, $this->end);
$lines[] = 'SUMMARY:'.$this->description;
$lines[] = 'END:VEVENT';
$lines[] = 'END:VCALENDAR';
return implode("\r\n", $lines);
}
public function download()
{
$data = $this->data();
header('Content-Disposition: attachment; filename="event.ics"');
header("Cache-Control: no-cache, must-revalidate");
header("Content-type:text/calendar");
header('Content-Length: '.strlen($data));
header('Connection: close');
echo $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment