Intrepid Studios, Inc. Blog: How To: Use PHP to Grab Event Date from Google Calendar RSS [Tips & Tricks] - Minneapolis, MN - .NET, XHTML, CSS, Adobe, web, graphic, media, and software design services

Learn more:

Overview

Intrepid Studios, Inc. is a software and web studio located in Minneapolis, MN that provides professional services for the web and desktop. We offer web design and development, graphic design, application design and development, and a host of other solutions.

Our Network

Previous Posts

 Subscribe in a reader

Powered by Blogger

Blog

Monday, August 4, 2008

How To: Use PHP to Grab Event Date from Google Calendar RSS [Tips & Tricks]

Recently I finished a site that required me to use Google Calendar to manage events. I thought that it would be nice to show what events are up and coming, so I used SimplePie to grab and show their calendar feed.

However, the RSS date was the date the event was added, not the date that the event happens. This makes sense but makes it a bit harder to be useful in the website.

I used some simple RegEx to get the event date… it’s rough but it works and I’ve tested it with events spanning multiple dates, all day, and specific times.

<dl>
    <?php
    // Feed: http://www.google.com/calendar/feeds/[google account]/public/basic?max-results=5
    foreach ($feed->get_items() as $item):
        
        // Grab When: date.
        $content = $item->get_content();
        $content = strip_tags($content);
        
        if(preg_match("/When: (.*!?)/", $content, $matches)) {
            $content = $matches[1];
        } else {
            $content = "No Date Found";
        }
    ?>
        <dt>Event:</dt>
        <dd><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></dd>
        <dt>Event Date:</dt>
        <dd><?php echo $content; ?></dd>
    <?php endforeach; ?>
</dl>

This assumes you have a SimplePie feed… you can just use the RegEx in any other implementation instead…

Labels: , ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home

Return to Navigation