-
-
Save theudebart/11191471 to your computer and use it in GitHub Desktop.
This script allows you to output a MySQL table content as an iCalendar 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 | |
// This work by Marcel Patzsch is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. | |
// To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/. | |
// Based on a work at https://gist.github.com/jakebellacera/635416. | |
// Notes: | |
// - Variables labeled with (!) are required. Non required variables can be set to "" or delete. | |
// | |
// - iCal requires a date format of "yyyymmddThhiissZ". The "T" and "Z" | |
// characters are not placeholders, just plain ol' characters. The "T" | |
// character acts as a delimeter between the date (yyyymmdd) and the time | |
// (hhiiss), and the "Z" states that the date is in UTC time. Note that if | |
// you don't want to use UTC time, you must set a timzone. | |
// | |
// - Read up on RFC 5545, the iCalendar specification. There is a lot of helpful | |
// info in there, such as formatting rules. There are also many more options | |
// to set, including alarms, invitees, busy status, etc. | |
// | |
// http://tools.ietf.org/html/rfc5545 | |
$Filename = "Calendar.ics"; //Filename (!) | |
$Calname = "Calendarname"; //Calendarname | |
$Timezone = "America/New_York"; //Timezone | |
$dbServer = "localhost"; //Database Address (!) | |
$dbUser = "dbuser"; //Database User (!) | |
$dbPass = "dbpass"; //Database Password (!) | |
$dbName = "dbname"; //Database Name (!) | |
$dbSelect = "Select * FROM Tablename"; //Database Select (!) | |
$dbTitleColumn = "Title"; //Table column name: Name of the Event (!) | |
$dbPriorityColumn = "Priority"; //Table column name: Priority of the Event | |
$dbLocationColumn = "Location"; //Table column name: Location/Address of the Event | |
$dbCreationDateColumn = "Created"; //Table column name: Creation Date of the Event | |
$dbStartDateColumn = "Start"; //Table column name: Start Date of the Event (!) | |
$dbEndDateColumn = "End"; //Table column name: End Date of the Event (!) | |
$dbDescriptionColumn = "Decription"; //Table column name: Description of the Event | |
$dbCommentColumn = "Comment"; //Table column name: Comment of the Event | |
header("Pragma: public"); | |
header("Expires: 0"); | |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | |
header("Cache-Control: private",false); | |
header("Content-Type: application/force-download"); | |
header("Content-Type: application/octet-stream"); | |
header("Content-Type: application/download"); | |
header("Content-Type: text/Calendar"); | |
header("Content-Disposition: attachment; filename=\"".$Filename."\";"); | |
header("Content-Description: File Transfer"); | |
header("Content-Transfer-Encoding: binary"); | |
echo "BEGIN:VCALENDAR\r\n"; //BEGIN CAL | |
echo "VERSION:2.0\r\n"; //VERSION: RFC 5545 section 3.7.4 | |
if (isset($Calname) && $Calname != "") | |
echo "X-WR-CALNAME:".$Calname."\r\n"; //NO RFC 5545 STANDARD | |
echo "PRODID:PHP\r\n"; //PRODUCT IDENTIFIER: RFC 5545 section 3.7.3 | |
echo "CALSCALE:GREGORIAN\r\n"; //CALSCALE: RFC 5545 section 3.7.1 | |
echo "METHOD:PUBLISH\r\n"; //METHOD: RFC 5545 section 3.7.2 | |
if (!mysql_connect($dbServer, $dbUser, $dbPass)) | |
die ("Connection to DB-Server failed."); | |
if (!mysql_select_db($dbName)) | |
die ("Database not found."); | |
mysql_query("SET CHARACTER SET 'utf8'"); | |
$query = mysql_query($dbSelect); | |
while($Column = mysql_fetch_array($query)){ | |
echo "BEGIN:VEVENT\r\n"; //BEGIN EVENT | |
echo "SUMMARY:".addslashes($Column[$dbTitleColumn])."\r\n"; //SUMMARY: RFC 5545 section 3.8.1.12 | |
if (isset($dbPriorityColumn) && $dbPriorityColumn != "") | |
echo "PRIORITY:".addslashes($Column[$dbPriorityColumn])."\r\n"; //PRIORITY: RFC 5545 section 3.8.1.9 | |
if (isset($dbLocationColumn) && $dbLocationColumn != "") | |
echo "LOCATION:".addslashes($Column[$dbLocationColumn])."\r\n"; //LOCATION: RFC5545 section 3.8.1.7 | |
if (isset($dbCreationDateColumn) && $dbCreationDateColumn != "") { //DATE-TIME STAMP: RFC 5545 section 3.8.7.2 | |
if (isset($Timezone) && $Timezone != "") | |
echo "DTSTAMP;TZID=".$Timezone.":".formateDate(strtotime($Column[$dbCreationDateColumn]),$Timezone)."\r\n"; | |
else | |
echo "DTSTAMP:".formateDate(strtotime($Column[$dbCreationDateColumn]),$Timezone)."\r\n"; | |
} | |
if (isset($Timezone) && $Timezone != "") //DATE-TIME START: RFC 5545 section 3.8.2.4 | |
echo "DTSTART;TZID=".$Timezone.":".formateDate(strtotime($Column[$dbStartDateColumn]),$Timezone)."\r\n"; | |
else | |
echo "DTSTART:".formateDate(strtotime($Column[$dbStartDateColumn]),$Timezone)."\r\n"; | |
if (isset($Timezone) && $Timezone != "") //DATE-TIME END: RFC 5545 section 3.8.2.2 | |
echo "DTEND;TZID=".$Timezone.":".formateDate(strtotime($Column[$dbEndDateColumn]),$Timezone)."\r\n"; | |
else | |
echo "DTEND:".formateDate(strtotime($Column[$dbEndDateColumn]),$Timezone)."\r\n"; | |
if (isset($dbDescriptionColumn) && $dbDescriptionColumn != "") | |
echo "DESCRIPTION:".addslashes($Column[$dbDescriptionColumn]). "\r\n"; //DESCRIPTION: RFC 5545 section 3.8.1.5 | |
if (isset($dbCommentColumn) && $dbCommentColumn != "") | |
echo "COMMENT:".addslashes($Column[$dbCommentColumn]). "\r\n"; //COMMENT: RFC 5545 section 3.8.1.4 | |
echo "END:VEVENT\r\n"; //END EVENT | |
} | |
echo "END:VCALENDAR"; //END CAL | |
//-----------------------------------------------------------------------------------------------------------------// | |
// Converts a unix timestamp to an ics-friendly format | |
// NOTE: "Z" means that this timestamp is a UTC timestamp. If $Timezone | |
// is set, the timestamp is interpreted as a local timestamp and DTEND, DTSTAMP and DTSTART | |
// are modifyed with TZID properties. Else the timestamp is interpreted as a UTC timestamp. | |
// | |
// Also note that we are using "H" instead of "g" because iCalendar's Time format | |
// requires 24-hour time (see RFC 5545 section 3.3.12 for info). | |
function formateDate($time,$timezone) { | |
if (isset($timezone) && $timezone != "") | |
return date('Ymd\THis', $time); | |
else | |
return date('Ymd\THis\Z', $time); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment