Skip to content

Instantly share code, notes, and snippets.

@benbyford
Created April 16, 2020 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbyford/ada3a229501c769880b0aa2b13276d3e to your computer and use it in GitHub Desktop.
Save benbyford/ada3a229501c769880b0aa2b13276d3e to your computer and use it in GitHub Desktop.
one event example of iCalendar implementation in PHP
<?php
function escapeString($string) {
return preg_replace('/([\,;])/','\\\$1', $string);
}
function dateToCal($timestamp) {
return date('Ymd', $timestamp) .'T'. date('His', $timestamp) . "Z";
}
$eol = "\r\n";
$load = "BEGIN:VCALENDAR" . $eol .
"VERSION:2.0" . $eol .
"PRODID:-//project/author//NONSGML v1.0//EN" . $eol .
"CALSCALE:GREGORIAN" . $eol .
"BEGIN:VEVENT" . $eol .
"DTSTAMP:" . dateToCal(time()) . $eol .
"DTSTART:" . dateToCal($from_timestamp) . $eol .
"DTEND:" . dateToCal($till_timestamp) . $eol .
"UID:" . $uid . $eol .
"DESCRIPTION:" . htmlspecialchars($description) . $eol .
"URL;VALUE=URI:" . htmlspecialchars($shortUrl) . $eol .
"SUMMARY:" . htmlspecialchars($title) . $eol .
"END:VEVENT" . $eol .
"END:VCALENDAR";
$filename="Event-".$title_no_space . ".ics";
// Set the headers
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: attachment; filename=' . $filename);
// Dump load
echo $load;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment