Designs | Resume | KamranAyub.com | email: ayubx003 (@umn.edu)

« New Website Release – Business Association for Multicultural Students [Web Design] | Main | Thoughts: On The Band Called “Queen” [Awesome Music] »

How To: Use PHP to Grab Event Date from Google Calendar RSS [Tips & Tricks] | Posted at 7:52 PM

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…

Filed Under: How To Tips and Tricks Web & Internet

Comments

I was searching the web for this information. Thanks for the tip.

This is an awesome trick. I actually needed this exact tip for a project that I am working on.

Thanks for sharing it (and for making the code accesible).

Do you recognize that it is the best time to get the home loans, which can make your dreams come true.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About the Author

Kamran
Divide by Zero is Kamran Ayub's personal blog. Kamran owns and operates Intrepid Studios, a web design and development firm based in Minneapolis. Despite the above picture, Kamran is usually an intelligent person. Please forgive him.

View Kamran Ayub's profile on LinkedIn

Add to Technorati Favorites