Import events from .ICS file to MySQL table using PHP

As we know .ICS file format is universal calendar format used to store calendar information within a text file. Lets create a PHP function which will import data from .ICS file and store in MySQL database.

Here we have included a php class file called icalendar.php having class called iCalendar, which will read the ICS file and convert the data to Array format. Once you get the data array you can either save it or you can show it in your webpage in tabular format.
import from ics

PHP Code

$records = mysql_num_rows(mysql_query("select * from events"));
if ($_POST['stage'] == 1) {
$ical = new iCalendar();
$filename = $_FILES['file1']['tmp_name'];
$ical_data = $ical->get_all_data();

$timezone = "{$ical_data['VCALENDAR']['X-WR-TIMEZONE']}";
if (function_exists('date_default_timezone_set'))
$strsql1 = "Insert into events (ID,StartDate,StartTime,EndDate,EndTime,Title,
Location,Description) values ";
if (!empty($ical_data['VEVENT'])) {
foreach ($ical_data['VEVENT'] as $key => $data) {

//get StartDate And StartTime
$start_dttimearr = explode('T', $data['DTSTART']);
$StartDate = $start_dttimearr[0];
$startTime = $start_dttimearr[1];

//get EndDate And EndTime
$end_dttimearr = explode('T', $data['DTEND']);
$EndDate = $end_dttimearr[0];
$EndTime = $end_dttimearr[1];

$strsql1 = rtrim($strsql1, ',');




connection.php file
$host="localhost";// Your Host name
$uname="******"; // Your Database  User name
$pass="*******"; // Your Database password 
$database = "********"; // Your Database name

or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected"); 
or die("database cannot be selected <br>");

Download Code Total Downloads: 1305

Other Related Posts

Export MySQL table to iCalendar (.ICS)
Export MySQL table to CSV using PHP
Read CSV File using PHP
comments powered by Disqus