Skip to content

Davin Studer

Google Calendar

So, I’ve been working on creating a php script that pulls Google Calendar data. I couldn’t find many examples of how to do it on the web. So, after much trial and error here is what I’ve come up with. Use as you wish.

<?php
/**********************************************************/
/* The username/password used to login to Google Calendar */
/*                                                        */
/* This is the only part you should have to edit in this  */
/* script.                                                */
/**********************************************************/
$google_username = '???';
$google_password = '???';

/**************************************************************************/
/* Include Zend framework and load libraries, used to interact with Gdata */
/* Get the Zend framework at http://framework.zend.com/download           */
/**************************************************************************/
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');

/******************/
/* Login to Gdata */
/******************/
$authService = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($google_username, $google_password, $authService);

/***********************************************************************/
/* Replaces [link:url]Text[/link] with a properly formatted anchor tag */
/* The url must have the http:// or https:// at the front              */
/*                                                                     */
/* Params:                                                             */
/*  $text - String of text                                             */
/***********************************************************************/
function createLink($text) {
    $pattern1 = '/\[\/link\]/';
    $pattern2 = '/(\[link:)([^\]]*)(\])/';

    $replacement1 = '</a>';
    $replacement2 = '<a href="$2" target="_blank" rel="noopener">';

    $text = preg_replace($pattern1 , $replacement1, $text);
    $text = preg_replace($pattern2 , $replacement2, $text);

    return $text;
}

/*****************************************************************************************/
/* Replaces [email:email address]Text[/email] with a properly formatted email anchor tag */
/*                                                                                       */
/* Params:                                                                               */
/*  $text - String of text                                                               */
/*****************************************************************************************/
function createEmail($text) {
    $pattern1 = '/\[\/email\]/';
    $pattern2 = '/(\[email:)([^\]]*)(\])/';

    $replacement1 = '</a>';
    $replacement2 = '<a href="mailto:$2">';

    $text = preg_replace($pattern1 , $replacement1, $text);
    $text = preg_replace($pattern2 , $replacement2, $text);

    return $text;
}

/*****************************************************/
/* Replaces line breaks in the text with <br /> tags */
/*                                                   */
/* Params:                                           */
/*  $text - String of text                           */
/*****************************************************/
function createLineBreaks($text) {
    $text = str_replace("\n", "<br />", $text);

    return $text;
}

/*****************************************************************/
/* This function applies the formatting functions defined above. */
/*                                                               */
/* Params:                                                       */
/*  $text - String of text                                       */
/*****************************************************************/
function formatText($text) {
    $text = createLink($text);
    $text = createEmail($text);
    $text = createLineBreaks($text);

    return $text;
}

/*****************************************************************/
/* This function formats the date/time shown to the user.        */
/*                                                               */
/* Params:                                                       */
/*  $start - Starting date/time                                  */
/*  $end - Ending date/time                                      */
/*****************************************************************/
function formatDate($start, $end) {
    if (date("H:i:s", $start) == "00:00:00" && date("H:i:s", $end) == "00:00:00") //All day event
    {
        $end = $end - 1; //For all day events the end day is one greater than it really is

        if(date("M j, Y", $start) == date("M j, Y", $end)) //Single day all day event
        {
            $date = date("M j, Y", $start);
        }
        else //All day event spanning multiple days
        {
            if (date("Y", $start) == date("Y", $end))
            {
                if (date("M", $start) == date("M", $end)) //All day event spanning multiple days in same month
                {
                    $date = date("M j", $start) . " - " . date("j, Y", $end);
                }
                else //All day event spanning multiple months
                {
                    $date = date("M j", $start) . " - " . date("M j, Y", $end);
                }
            }
            else // All day event spanning multiple years
            {
                $date = date("M j, Y", $start) . " - " . date("M j, Y", $end);
            }
        }
    }
    else //Time range event
    {
        if(date("M j, Y", $start) == date("M j, Y", $end)) //Single day range event
        {
            $date = date("M j, Y (g:i a", $start) . " - " . date("g:i a)", $end);
        }
        else //Range event spanning multiple days
        {
            if (date("Y", $start) == date("Y", $end))
            {
                if (date("M", $start) == date("M", $end)) //Range event spanning multiple days in same month
                {
                    $date = date("M j (g:i a)", $start) . " - " . date("M j (g:i a), Y", $end);
                }
                else //Range  event spanning multiple months
                {
                    $date = date("M j (g:i a)", $start) . " - " . date("M j (g:i a), Y", $end);
                }
            }
            else //Range event spanning multiple years
            {
                $date = date("M j, Y (g:i a)", $start) . " - " . date("M j, Y (g:i a)", $end);
            }
        }
    }

    return $date;
}

/******************************************************************/
/* Create a definition list of the events in human-readable form. */
/* You can use CSS to style the list.                             */
/*                                                                */
/* Params:                                                        */
/* $client - GData $httpClient created above                      */
/*  $feed - The GData feed to read                                */
/******************************************************************/
function listEvents($client, $feed)
{
    $gdataCal = new Zend_Gdata_Calendar($client);
    $eventFeed = $gdataCal->getCalendarEventFeed($feed);

    $html = $html . "<dl>\n";

    foreach ($eventFeed as $event)
    {
        foreach ($event->when as $when)
        {
            $start = strtotime($when->startTime);
            $end = strtotime($when->endTime);
        }
        $date = formatDate($start, $end);

        foreach ($event->where as $where)
        {
            $location = $where->valueString;
        }

        $title = $event->title->text;
        if ($location != "")
        {
            $title = "<a href=\"" . $location . "\" target=\"_blank\">" . $title . "</a>";
        }
        $content = formatText($event->content->text);

        $html = $html . " <dt><span class=\"eventdate\">" . $date . "</span><span class=\"eventtitle\"> - " . $title . "</span></dd>\n";
        $html = $html . " <dd><span class=\"eventcontent\">" . $content . quot;</span></dd>\n";
    }

    $html = $html . "</dl>\n";

    return $html;
}
?>
<html>
    <body>
<?php
Echo listEvents($httpClient, "http://www.google.com/calendar/feeds/yourfeedhere");
?>
    </body>
</html>

Get Firebug

For those of you who are web developers there is a great extension for Firefox that has just recently come out. I’ve used it for about a week, and I am in love with it … not as much as I love my wife though. It is the greatest thing for web developers since Jolt. By the way you may want to check out their site, since it has some information about the plug in that you may not realize just playing around with it at first … there are lots of features. Go get it and try it for yourself.

Firebug