You are here

function addtocal_rfc_3339_date in Add to Cal 7

Returns an array containing RFC 3339 formatted start and end dates.

Parameters

$start: Start date

$end: End date

$timezone: Timezone for the supplied dates

Return value

array

4 calls to addtocal_rfc_3339_date()
addtocal_download_ics in ./addtocal.module
Outputs an ICS file containing event information for the selected entity. Called by hook_menu.
addtocal_google_link in ./addtocal.module
Redirects to a Google Calendar event. Called by hook_menu().
addtocal_render in ./addtocal.module
Generate a render array for the addtocal widget.
addtocal_yahoo_link in ./addtocal.module
Redirects to a Yahoo Calendar event. Called by hook_menu().

File

./addtocal.module, line 532
addtocal.module General functions and hook implementations.

Code

function addtocal_rfc_3339_date($start, $end, $timezone) {
  if (!$end) {
    $end = $start;
  }
  $tz_utc = new DateTimeZone('UTC');

  // When $timezone is an empty string no default is used and we see a Unknown or bad timezone () in DateTimeZone->__construct() error.
  if (empty($timezone)) {
    $tz = NULL;
  }
  else {
    $tz = new DateTimeZone($timezone);
  }
  $startDate = new DateTime($start, $tz);
  $startDate
    ->setTimezone($tz_utc);
  $endDate = new DateTime($end, $tz);
  $endDate
    ->setTimezone($tz_utc);
  $start_timestamp = $startDate
    ->getTimestamp();
  $end_timestamp = $endDate
    ->getTimestamp();
  $diff_timestamp = $end_timestamp - $start_timestamp;
  $start_date = gmdate('Ymd', $start_timestamp) . 'T' . gmdate('His', $start_timestamp) . 'Z';
  $local_start_date = date('Ymd', $start_timestamp) . 'T' . date('His', $start_timestamp) . '';
  $end_date = gmdate('Ymd', $end_timestamp) . 'T' . gmdate('His', $end_timestamp) . 'Z';
  $local_end_date = date('Ymd', $end_timestamp) . 'T' . date('His', $end_timestamp) . '';
  $diff_hours = str_pad(round($diff_timestamp / 60 / 60), 2, '0', STR_PAD_LEFT);
  $diff_minutes = str_pad(abs(round($diff_timestamp / 60) - $diff_hours * 60), 2, '0', STR_PAD_LEFT);
  return array(
    'start' => $start_date,
    'end' => $end_date,
    'both' => $start_date . '/' . $end_date,
    'local_start' => $local_start_date,
    'local_end' => $local_end_date,
  );
}