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…

0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home