Last active
December 16, 2015 20:39
-
-
Save artlung/5493859 to your computer and use it in GitHub Desktop.
A FILE TO CREATE A DOWNLOADABLE ICS FILE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// based on question on AskMefi: | |
// http://ask.metafilter.com/240123/Create-downloadable-iCal-files-in-PHPMySQLBootstrap | |
// validate the result at http://severinghaus.org/projects/icv/ | |
// pass along an id on the url string, but make sure it's a number | |
// make_my_ical_file.php?id=45 | |
$id = (isset($_GET[id])) ? (int)$_GET['id'] : 0; | |
// This is just dummy data | |
$event_row = array( | |
array( | |
'eventDate' => '2013-12-04', | |
'starttime' => '2013-12-04 11:00:00', | |
'endtime' => '2013-12-05 01:00:00', | |
'eventName' => 'The Special Event Name!', | |
'hotelSpace' => 'Hotel Example', | |
'eventDescription' => 'This is a helluvan event', | |
'timezone' => 'America/Chicago', | |
), | |
); | |
// Set this to true to use real data | |
$use_database = false; | |
if ($use_database) { | |
// actually run a query | |
$query = "SELECT eventDate | |
, eventStart | |
, eventStart as starttime | |
, eventEnd as endtime | |
, eventName | |
, hotelSpace | |
, eventDescription | |
, 'America/Chicago' as timezone | |
FROM events | |
WHERE id = {$id}"; | |
$result = @mysql_query($query); | |
$event_row = array(); | |
// really should only be one result | |
while ($row = mysql_fetch_assoc ($result)) { | |
$event_row[] = $row; | |
} | |
} | |
define("DATE_ICAL", "Ymd\THis"); | |
// really there should be only one row | |
foreach ($event_row as $row) { | |
$icalstarttime = date(DATE_ICAL, strtotime( $row['starttime'])); | |
$icalendtime = date(DATE_ICAL, strtotime( $row['endtime'])); | |
$eventid = md5(uniqid(mt_rand(), true)) . "@" . $_SERVER['SERVER_NAME']; | |
$dateStamp = date(DATE_ICAL, strtotime("now")); | |
$safe_event_name = preg_replace("/[^\da-z]/i", '_', trim(strtolower($row['eventName']))); | |
$icalrows = array(); | |
$icalrows[] = "BEGIN:VCALENDAR"; | |
$icalrows[] = "VERSION:2.0"; | |
$icalrows[] = "PRODID:-//hacksw/handcal//NONSGML v1.0//EN"; | |
$icalrows[] = "BEGIN:VEVENT"; | |
$icalrows[] = "UID:{$eventid}"; | |
$icalrows[] = "DTSTAMP:{$dateStamp}"; | |
$icalrows[] = "DTSTART;TZID={$row['timezone']}:{$icalstarttime}"; | |
$icalrows[] = "DTEND;TZID={$row['timezone']}:{$icalendtime}"; | |
$icalrows[] = "SUMMARY:{$row['eventName']} at {$row['eventDescription']} at {$row['hotelSpace']}"; | |
$icalrows[] = "END:VEVENT"; | |
$icalrows[] = "END:VCALENDAR"; | |
//you can set the Content-Type: to text/plain to debug | |
//header('Content-type: text/plain; charset=utf-8'); | |
header("Content-Type: text/calendar; charset=utf-8"); | |
header("Content-Disposition: inline; filename={$safe_event_name}.ics"); | |
echo implode("\r\n", $icalrows); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment