You are here

function _social_event_format_date in Open Social 8.6

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.module \_social_event_format_date()
  2. 8 modules/social_features/social_event/social_event.module \_social_event_format_date()
  3. 8.2 modules/social_features/social_event/social_event.module \_social_event_format_date()
  4. 8.3 modules/social_features/social_event/social_event.module \_social_event_format_date()
  5. 8.4 modules/social_features/social_event/social_event.module \_social_event_format_date()
  6. 8.5 modules/social_features/social_event/social_event.module \_social_event_format_date()
  7. 8.7 modules/social_features/social_event/social_event.module \_social_event_format_date()
  8. 8.8 modules/social_features/social_event/social_event.module \_social_event_format_date()
  9. 10.3.x modules/social_features/social_event/social_event.module \_social_event_format_date()
  10. 10.0.x modules/social_features/social_event/social_event.module \_social_event_format_date()
  11. 10.1.x modules/social_features/social_event/social_event.module \_social_event_format_date()
  12. 10.2.x modules/social_features/social_event/social_event.module \_social_event_format_date()

Formats the event start end date.

1 call to _social_event_format_date()
social_event_preprocess_node in modules/social_features/social_event/social_event.module
Prepares variables for node templates.

File

modules/social_features/social_event/social_event.module, line 309
The Social event module.

Code

function _social_event_format_date($event, $view_mode) {
  $event_date = '';

  // This will get the users timezone, which is either set by the user
  // or defaults back to the sites timezone if the user didn't select any.
  $timezone = drupal_get_user_timezone();

  // Timezone that dates should be stored in.
  $utc_timezone = DateTimeItemInterface::STORAGE_TIMEZONE;

  // Get start and end dates.
  if ($start_date_field = $event->field_event_date) {
    if (!empty($start_date_field->value)) {

      // Since dates are stored as UTC, we will declare our event values
      // as UTC. So we can actually calculate them back to the users timezone.
      // This is necessary because we do not store the event value as being UTC
      // so declaring it with setTimezone will result in wrong values.
      $start_datetime = new DateTime($start_date_field->value, new DateTimeZone($utc_timezone));
      $start_datetime
        ->setTimezone(new DateTimeZone($timezone));
      $start_datetime = $start_datetime
        ->getTimestamp();
    }
  }
  if ($end_date_field = $event->field_event_date_end) {
    if (!empty($end_date_field->value)) {

      // Since dates are stored as UTC, we will declare our event values
      // as UTC. So we can actually calculate them back to the users timezone.
      // This is necessary because we do not store the event value as being UTC
      // so declaring it with setTimezone will result in wrong values.
      $end_datetime = new DateTime($end_date_field->value, new DateTimeZone($utc_timezone));

      // We now calculate it back to the users timezone.
      $end_datetime
        ->setTimezone(new DateTimeZone($timezone));
      $end_datetime = $end_datetime
        ->getTimestamp();
    }
  }

  // Get date and time formats.
  $date_formatter = \Drupal::service('date.formatter');
  $date_format = $view_mode === 'hero' ? 'social_long_date' : 'social_medium_extended_date';
  $time_format = 'social_time';
  if (!empty($start_datetime)) {
    $start_date = $date_formatter
      ->format($start_datetime, $date_format);

    // Default time should not be displayed.
    $start_time = $date_formatter
      ->format($start_datetime, 'custom', 'i') === '01' ? '' : $date_formatter
      ->format($start_datetime, $time_format);
    if (!empty($end_datetime)) {
      $end_date = $date_formatter
        ->format($end_datetime, $date_format);

      // Default time should not be displayed.
      $end_time = $date_formatter
        ->format($end_datetime, 'custom', 'i') === '01' ? '' : $date_formatter
        ->format($end_datetime, $time_format);
    }

    // Date are the same or there are no end date.
    if (empty($end_datetime) || $start_datetime == $end_datetime) {
      $event_date = empty($start_time) ? $start_date : "{$start_date} {$start_time}";
    }
    elseif (date(DateTimeItemInterface::DATE_STORAGE_FORMAT, $start_datetime) == date(DateTimeItemInterface::DATE_STORAGE_FORMAT, $end_datetime)) {
      $event_date = "{$start_date} {$start_time} - {$end_time}";
    }
    elseif (!empty($end_datetime)) {
      $event_date = "{$start_date} {$start_time} - {$end_date} {$end_time}";
    }
  }
  return $event_date;
}