You are here

function views_timelinejs_convert_to_csv in Views TimelineJS integration 7

Converts a given time and date into CSV format with Drupals timezone.

Parameters

string $value: The date to be converted.

string $date_format: The format of the date.

string $timezone: The timezone of the date.

string $db_timezone: The Drupal timezone.

Return value

string CSV formatted date.

4 calls to views_timelinejs_convert_to_csv()
views_timelinejs_date_iso_source_date_field_conversion in plugins/date_sources/date_iso_field.inc
Converts ISO date field formats, forces TimelineJS to ignore browser TZ.
views_timelinejs_date_source_date_field_conversion in plugins/date_sources/date_field.inc
Converts date field formats, forces TimelineJS to ignore browser timezone.
views_timelinejs_date_source_post_date_conversion in plugins/date_sources/post_date.inc
Converts post date formats, forces TimelineJS to ignore browser TZ.
views_timelinejs_date_stamp_source_date_field_conversion in plugins/date_sources/date_stamp_field.inc
Converts date stamp field formats, forces TimelineJS to ignore browser TZ.

File

./views_timelinejs.module, line 128
Views TimelineJS API, theming, libraries, etc.

Code

function views_timelinejs_convert_to_csv($value, $date_format, $timezone, $db_timezone, $granularity) {

  // To avoid errors, if value is empty set it to unix epoch.
  if (empty($value)) {
    return "FALSE";
  }
  $timezone = new DateTimezone($timezone);
  $db_timezone = new DateTimezone($db_timezone);
  switch ($date_format) {
    case 'datestamp':
    case 'timestamp':
      $date = new DateObject();
      $date
        ->setTimezone($db_timezone);
      $date
        ->setTimestamp($value);
      break;
    default:
      $date = new DateObject($value, $db_timezone);
      break;
  }
  $format = array();
  if (empty($granularity)) {
    $format = array(
      'Y,m,d,H,i',
    );
  }
  else {
    if (is_string($granularity['year']) && $granularity['year'] == 'year') {
      $format[] = 'Y';
    }
    if (is_string($granularity['month']) && $granularity['month'] == 'month') {
      $format[] = 'm';
    }
    if (is_string($granularity['day']) && $granularity['day'] == 'day') {
      $format[] = 'd';
    }
    if (is_string($granularity['hour']) && $granularity['hour'] == 'hour') {
      $format[] = 'H';
    }
    if (is_string($granularity['minute']) && $granularity['minute'] == 'minute') {
      $format[] = 'i';
    }
    if (is_string($granularity['second']) && $granularity['second'] == 'second') {
      $format[] = 's';
    }
  }
  $format = implode(',', $format);
  $date
    ->setTimezone($timezone);
  return $date
    ->format($format);
}