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…

Comments
I was searching the web for this information. Thanks for the tip.
Posted by: Betty
On February 23, 2009 12:00 AM
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).
Posted by: Super Jumbo Mortgage
On March 17, 2009 1:42 AM
Do you recognize that it is the best time to get the home loans, which can make your dreams come true.
Posted by: JulietMUNOZ
On December 6, 2011 3:06 AM