Preprocess Google Calendar Feed

2014-01-03 | Category: General (47)

A common use case for our service is embedding a google calendar feed as box into a website. There is however one issue with those feeds: their entry date doesn't contain the event date - instead it's when the event was created (or last updated).

You can work around that by adding &sortorder=ascending&orderby=starttime parameters - and use "natural" sort order in the rssinclude box options.

This however doesn't work anymore if you try to combine multiple feeds and need to sort the events by date.

Preprocessing the feed using a custom php script on your own server is an easy workaround for this. Basic knowledge about php should be enough to get this running.

So if you have an Webserver where you can run custom php scripts, upload the following php script and use that script url as feed url in include.

<?php
$url = 'https://www.google.com/calendar/feeds/......';
$xml = simplexml_load_file($url);
foreach ($xml->entry as $entry) {
 
    unset($entry->published); //remove published from xml
 
    //parse "When: " from content using regular expression
    if (preg_match('#When: (.*)#', $entry->content, $m)) {
        $date = $m[1];
        $date = preg_replace('# to .*#', '', $date); //remove to time
        $date = date('c', strtotime($date)); //reformat date
 
        $entry->updated = $date; //assign date to xml
    }
}
header('Content-Type: application/atom+xml; charset=UTF-8');
echo $xml->asXml();
 

If you have troubles getting the script running feel free to contact us.