Skip to content

Instantly share code, notes, and snippets.

@Feddroid
Forked from petertwise/addtogcal.php
Created June 15, 2018 17:10
Show Gist options
  • Save Feddroid/db7991c98f9d42d60bd9229091b64980 to your computer and use it in GitHub Desktop.
Save Feddroid/db7991c98f9d42d60bd9229091b64980 to your computer and use it in GitHub Desktop.
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') );
*
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment