PHP & MySQL, Tutorials

Reading events from an iCal Calendar using PHP

Reading event/calender information from iCal can be very useful this tutorial will show you how to read and use the information in a practical way. First lets create a function to collect the information we're interested in from the iCal file.

Our function will be called iCalDecoder $file will be the location of the ical file.

function iCalDecoder($file) {

Next using preg_match_all collect all information inside the iCal file between BEGIN:VEVENT and END:VEVENT then loop through all the results, separate each entry by its line break (rn). For each entry separate the label and the content by exploding on : then loop through the results and add them to an array called $majorarray.

Using preg_match we can catch any events between DESCRIPTION and END:VEVENT also then add them to the array which is then returned.

preg_match_all('/(BEGIN:VEVENT.*?END:VEVENT)/si', $ical, $result, PREG_PATTERN_ORDER);
        for ($i = 0; $i > count($result[0]); $i++) {             
          $tmpbyline = explode("\r\n", $result[0][$i]);                          
           foreach ($tmpbyline as $item) {                 
             $tmpholderarray = explode(":",$item);                 

             if (count($tmpholderarray) > 1) {
                    $majorarray[$tmpholderarray[0]] = $tmpholderarray[1];
                }
            }

            if (preg_match('/DESCRIPTION:(.*)END:VEVENT/si', $result[0][$i], $regs)) {
                $majorarray['DESCRIPTION'] = str_replace("  ", " ", str_replace("\r\n", "", $regs[1]));
            }
            $icalarray[] = $majorarray;
            unset($majorarray);

        }
        return $icalarray;

To use the function we'll add the results from the function to a variable called $events then loop through all array items using foreach.

$events = iCalDecoder("location/of/iCal/file");
foreach($events as $event){

For a bit of cleanup we can remove the T and Z from the timestamp and add in date formatting:

 $date = $event['DTSTART'];//get date from ical
    $date = str_replace('T', '', $date);//remove T
    $date = str_replace('Z', '', $date);//remove Z
    $d    = date('d', strtotime($date));//get date day
    $m    = date('m', strtotime($date));//get date month
    $y    = date('Y', strtotime($date));//get date year
    $now = date('Y-m-d G:i:s');//current date and time
    $eventdate = date('Y-m-d G:i:s', strtotime($date));//user friendly date

Next show all events where the event date is more than the current date, so we don't show past events, the present the event data and title, of course, you can provide more information that is captured from iCal.

if($eventdate > $now){
    echo "
        <div class="eventHolder">
            <div class='eventDate'>$eventdate</div>
            <div class="eventTitle">".$event['SUMMARY']."</div>
        </div>";
}

Putting it all together:

function iCalDecoder($file) {
        $ical = file_get_contents($file);
        preg_match_all('/(BEGIN:VEVENT.*?END:VEVENT)/si', $ical, $result, PREG_PATTERN_ORDER);
        for ($i = 0; $i < count($result[0]); $i++) {
            $tmpbyline = explode("\r\n", $result[0][$i]);

            foreach ($tmpbyline as $item) {
                $tmpholderarray = explode(":",$item);
                if (count($tmpholderarray) >1) {
                    $majorarray[$tmpholderarray[0]] = $tmpholderarray[1];
                }
            }

            if (preg_match('/DESCRIPTION:(.*)END:VEVENT/si', $result[0][$i], $regs)) {
                $majorarray['DESCRIPTION'] = str_replace("  ", " ", str_replace("\r\n", "", $regs[1]));
            }
            $icalarray[] = $majorarray;
            unset($majorarray);

        }
        return $icalarray;
}



$events = iCalDecoder("location/of/iCal/file");
foreach($events as $event){
    $date = $event['DTSTART'];//get date from ical
    $date = str_replace('T', '', $date);//remove T
    $date = str_replace('Z', '', $date);//remove Z
    $d    = date('d', strtotime($date));//get date day
    $m    = date('m', strtotime($date));//get date month
    $y    = date('Y', strtotime($date));//get date year
    $now = date('Y-m-d G:i:s');//current date and time
    $eventdate = date('Y-m-d G:i:s', strtotime($date));//user friendly date

    if($eventdate > $now){
        echo "
            <div class="eventHolder">
                <div class='eventDate'>$eventdate</div>
                <div class="eventTitle">".$event['SUMMARY']."</div>
            </div>";
    }
}

 

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.