Skip to content

Instantly share code, notes, and snippets.

@darkterminal
Forked from axwax/addToGoogleCalendar.php
Created September 19, 2018 14:05
Show Gist options
  • Save darkterminal/183394d9f316221a34eb974d537a7253 to your computer and use it in GitHub Desktop.
Save darkterminal/183394d9f316221a34eb974d537a7253 to your computer and use it in GitHub Desktop.
add event to google calendar
<?php
// add event to google calendar
echo '<a href="' . addToGoogleCalendar("Let's test this", "next wednesday 2pm", "next wednesday 4pm", "right here, right now", "bla blah rhubarb http://axwax.de") . '">next weds 2pm</a><br/>';
echo '<a href="' . addToGoogleCalendar("Breakfast at Tiffany's", "tomorrow 8am") . '">Breakfast at Tiffany\'s</a>';
function addToGoogleCalendar($title='', $startdate='', $enddate='', $location='', $details='')
{
$startdate = ($startdate ? $startdate : time());
$startdate = (is_numeric($startdate) ? $startdate : strtotime($startdate));
$enddate = ($enddate ? $enddate : $startdate + 3600);
$enddate = (is_numeric($enddate) ? $enddate : strtotime($enddate));
$google_url = "http://www.google.com/calendar/event";
$action = "?action=TEMPLATE";
$title = ( $title ? ("&text=" . urlencode($title)) : "") ;
$dates = "&dates=" . getIcalDate($startdate) . "Z/" . getIcalDate($enddate) . "Z";
$location = ( $location ? ("&location=" . urlencode($location)) : "") ;
$details = ( $details ? ("&details=" . urlencode($details)) : "") ;
$out = $google_url . $action . $title . $dates . $location . $details;
return $out;
}
function getIcalDate($time, $incl_time = true)
{
return $incl_time ? date('Ymd\THis', $time) : date('Ymd', $time);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment