Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to create Add to Google Calendar link
<?php
function squarecandy_add_to_gcal(
$name,
$startdate,
$enddate = false,
$description = false,
$location = false,
$allday = false,
$linktext = 'Add to gCal',
$classes = array('gcal-button, button')
) {
// calculate the start and end dates, convert to ISO format
if ($allday) {
$startdate = date('Ymd',strtotime($startdate));
}
else {
$startdate = date('Ymd\THis',strtotime($startdate));
}
if ($enddate && !empty($enddate) && strlen($enddate) > 2) {
if ($allday) {
$enddate = date('Ymd',strtotime($enddate . ' + 1 day'));
}
else {
$enddate = date('Ymd\THis',strtotime($enddate));
}
}
else {
$enddate = date('Ymd\THis',strtotime($startdate . ' + 2 hours'));
}
// build the url
$url = 'http://www.google.com/calendar/event?action=TEMPLATE';
$url .= '&text=' . rawurlencode($name);
$url .= '&dates=' . $startdate . '/' . $enddate;
if ($description) {
$url .= '&details=' . rawurlencode($description);
}
if ($location) {
$url .= '&location=' . rawurlencode($location);
}
// build the link output
$output = '<a href="' . $url . '" class="' . implode(' ',$classes) . '">'.$linktext.'</a>';
return $output;
}
/********************
*
* Example Usage:
*
* echo squarecandy_add_to_gcal('Example Event', 'June 30, 2017 8:00pm');
* echo squarecandy_add_to_gcal('Example Event', 'June 30, 2017 8:00pm', 'July 2, 2017 10:00am', 'This is my detailed event description', '1600 Pennsylvania Ave NW, Washington, DC 20500');
* echo squarecandy_add_to_gcal('Example Event', 'June 30, 2017', 'July 2, 2017', 'This is my detailed event description', '1600 Pennsylvania Ave NW, Washington, DC 20500', true, 'gCal+', array('my-custom-class') );
*
*/
?>
@nmj18txstate

This comment has been minimized.

Copy link

nmj18txstate commented Jun 6, 2017

Please find the below screenshots, i executed the function in netbeans and in my webbrowser but i'm getting no output on screen. Please help me figure out the problem. Am i missing something apart from this ?
gcal
gcal1

@squarecandy

This comment has been minimized.

Copy link
Owner Author

squarecandy commented Aug 22, 2017

@nmj18txstate - see the example usage section in the comments at the bottom of the file. The function doesn't print anything to the screen by itself. You must call it with the details of your particular event within your own custom code.

@vanesa

This comment has been minimized.

Copy link

vanesa commented Aug 30, 2017

Hi!
do you know what the attribute for the 'reminder' or 'notification' option is? I would love to be able to set up at least one notification setting.
Thank you!

@squarecandy

This comment has been minimized.

Copy link
Owner Author

squarecandy commented Dec 18, 2017

@vanesa - unfortunately I think you might need to use the Google API v3 for that, which requires connecting with an API key or other authentication and is overwhelmingly more annoying than just setting up a url...

@dawogfather

This comment has been minimized.

Copy link

dawogfather commented May 8, 2019

Is there anything like this that you can provide a complete iCal instead of a single event?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.