JoshuaDoshua
8/3/2018 - 1:18 PM

Function to create Add to Google Calendar link

Function to create Add to Google Calendar link

<?php
function squarecandy_add_to_gcal(
  $name,
  $startDate,
  $endDate = false,
  $isAllDay = false,
  $description = null,
  $location = null
) {

  $shortFormat = "Ymd";
  $longFormat = "Ymd\THis";

  $startDate = strtotime($startDate);
  $endDate = strtotime($endDate);
  
  $startDate = $isAllDay 
    ? date($shortFormat, $startDate)
    : date($longFormat, $startDate);

  $endDate = $isAllDay
    ? date($shortFormat, $endDate)
    : date($longFormat, $endDate);

  $url = 'http://www.google.com/calendar/event?';
  $url .= http_build_query([
    'action' => 'TEMPLATE',
    'dates' => $startdate . '/' . $enddate,
    'details' => $description,
    'location' => $location
  ]);

  return $url;
}