fastread homefastrread library fastread menu

PHP Tutorial : Export MySQL table to iCalendar (.ICS) using PHP

Tutorial by:Admin      Date: 2016-04-14 02:30:46

❰ Previous Next ❱

What is .ICS File

The iCalendar (.ICS) file format is universal calendar format used to store calendar information within a text file. It helps users to publish and share calendar information, meeting requests and tasks to other Internet users via email or website.
 

How to open .ICS File

.ICS files can open with Microsoft's Outlook, Apple's iCalendar, IBM Lotus Notes and Google Calendar etc.

How to export MySql data to .ICS File

Lets write a simple php program to export data from MySql table to ICS file.

PHP Code

<?php

include("includes/connection.php");

# Select Database
$sql = mysql_query( "SELECT * FROM events");

$ics_data = "BEGIN:VCALENDAR\n";
$ics_data .= "VERSION:2.0\n";
$ics_data .= "PRODID:PHP\n";
$ics_data .= "METHOD:PUBLISH\n";
$ics_data .= "X-WR-CALNAME:Schedule\n";

# Change the timezone if needed
$ics_data .= "X-WR-TIMEZONE:Asia/Kolkata\n";
$ics_data .= "BEGIN:VTIMEZONE\n";
$ics_data .= "TZID:Asia/Kolkata\n";
$ics_data .= "BEGIN:DAYLIGHT\n";
$ics_data .= "TZOFFSETFROM:-0500\n";
$ics_data .= "TZOFFSETTO:-0400\n";
$ics_data .= "DTSTART:1403086496\n";
$ics_data .= "RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU\n";
$ics_data .= "TZNAME:EDT\n";
$ics_data .= "END:DAYLIGHT\n";
$ics_data .= "BEGIN:STANDARD\n";
$ics_data .= "TZOFFSETFROM:-0400\n";
$ics_data .= "TZOFFSETTO:-0500\n";
$ics_data .= "DTSTART:1403086496\n";
$ics_data .= "RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU\n";
$ics_data .= "TZNAME:EST\n";
$ics_data .= "END:STANDARD\n";
$ics_data .= "END:VTIMEZONE\n";

while ($event_details = mysql_fetch_assoc($sql)) {
$id = $event_details['ID'];
$start_date = $event_details['StartDate'];
$start_time = $event_details['StartTime'];
$end_date = $event_details['EndDate'];
$end_time = $event_details['EndTime'];
$name = $event_details['Title'];
$location = $event_details['Location'];
$description = $event_details['Description'];

# Replace HTML tags
$search = array("/<br>/","/&amp;/","/&rarr;/","/&larr;/","/,/","/;/");
$replace = array("\\n","&","-->","<--","\\,","\\;"); 

$name = preg_replace($search, $replace, $name);
$location = preg_replace($search, $replace, $location);
$description = preg_replace($search, $replace, $description);

# Change TimeZone if needed
$ics_data .= "BEGIN:VEVENT\n";
$ics_data .= "DTSTART;TZID=Asia/Kolkata:".$start_date."T".$start_time."\n";
$ics_data .= "DTEND:" . $end_date . "T" . $end_time . "\n";
$ics_data .= "DTSTAMP:" . date('Ymd') . "T" . date('His') . "Z\n";
$ics_data .= "LOCATION:" . $location . "\n";
$ics_data .= "DESCRIPTION:" . $description . "\n";
$ics_data .= "SUMMARY:" . $name . "\n";
$ics_data .= "UID:" . $id . "\n";
$ics_data .= "SEQUENCE:0\n";
$ics_data .= "END:VEVENT\n";
}
$ics_data .= "END:VCALENDAR\n";

# Download the File
$filename = "event_calendar.ics";
header("Content-type:text/calendar");
header("Content-Disposition: attachment; filename=$filename");
echo $ics_data;
exit;
?>


connection.php file

<?php
$host="localhost";// Your Host name
$uname="******"; // Your Database  User name
$pass="*******"; // Your Database password 
$database = "********"; // Your Database name

$connection=mysql_connect($host,$uname,$pass) 
or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected <br>");
?>

❰ Previous Next ❱


PHP Tutorial

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website