Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Create a Google Calendar (0 Comments)
Admin: Posted Date: March 3, 2010

Google Calendar is a free and fully featured web based calendar solution. The calendar information is kept private by default, but you can also make a calendar public. This is great if you have a list of public events that you want to manage and display.

Create a Google Calendar widget with PHP

Introduction

Google Calendar is a free and fully featured web based calendar solution. The calendar information is kept private by default, but you can also make a calendar public. This is great if you have a list of public events that you want to manage and display. Here we will use PHP to retrieve the events from a public Google Calendar and display them on a web page.

Google Calendar PHP Screenshot

Step 1 - Create a public calendar

Log into Google Calendar and create a new calendar using the link highlighted in the screenshot below.

Google Calendar PHP Screenshot

Make sure you tick Make this calendar public.

Google Calendar PHP Screenshot

Add some events to the public calendar.

Google Calendar PHP Screenshot

Click the drop down arrow next to the public calendar and click Calendar Settings.

Google Calendar PHP Screenshot

Make a note of the Calendar ID. We will need this ID later on.

Google Calendar PHP Screenshot

Step 2 - Get the Google Data PHP Client Library

Download a copy of the Google Data PHP Client Library from here. Extract it and add a reference to the library from your PHP.INI file, using the include_path option.

Google Calendar PHP Screenshot

Step 3 - Define the query URL

We need to know the URL to contact in order to query the calendar. This is where the Calendar ID from step 1 is used.

$calendarID = "7p1r13309m2tpe7h1p3o4o7u34@group.calendar.google.com";

$googleFeedsURL = "http://www.google.com/calendar/feeds/" . $calendarID . "/public/full";

Step 4 - Load the Zend library and classes

The following code loads the Zend PHP library and a number of classes that we will use later on.

require_once 'Zend/Loader.php';

Zend_Loader::loadClass('Zend_Gdata');

Zend_Loader::loadClass('Zend_Gdata_Query');

Zend_Loader::loadClass('Zend_Gdata_Calendar');

Step 5 - Query the web service

Here we use the URL defined in step 3 to get the calendars data and store it in a variable called eventFeed.

$query = new Zend_Gdata_Query($googleFeedsURL);

$gdClient = new Zend_Gdata_Calendar();

$eventFeed = $gdClient->getCalendarEventFeed($query);

Step 6 - Display the results

To display the calendar details we just need to loop over the events contained in the eventFeed variable. Notice that we use the date_parse_from_format function to break down the date string returned by the web service. This allows us to display the date in a more readable form.

foreach ($eventFeed as $event)

{

echo '<p><a href="' . $event->link[0]->href . '">';

echo $event->title->text;

echo '</a></p>';

foreach ($event->when as $when)

{

$startData = date_parse_from_format("Y-m-d\TH:i:s:uO", $when->startTime);

$endData = date_parse_from_format("Y-m-d\TH:i:s:uO", $when->endTime);

echo '<p>' .

$startData['day'] . '-' .

$startData['month'] . '-' .

$startData['year'] . ' ' .

$startData['hour'] . ':' .

sprintf("%02d", $startData['minute']) .

'<br/>' .

'To' .

'<br/>' .

$endData['day'] . '-' .

$endData['month'] . '-' .

$endData['year'] . ' ' .

$endData['hour'] . ':' .

sprintf("%02d", $endData['minute']) .

'</p>';

}

}

Conclusion

You could modify this code to display events between certain days, or filter events based on a string. The benefit of using Google Calendar as the back end database is that entering and maintaining a calendar is a piece of cake. The only code you have to write is the code above to display the events on your site.

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: