function _social_event_format_date in Open Social 8
Same name and namespace in other branches
- 8.9 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.2 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.3 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.4 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.5 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.6 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.7 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 8.8 modules/social_features/social_event/social_event.module \_social_event_format_date()
- 10.3.x modules/social_features/social_event/social_event.module \_social_event_format_date()
- 10.0.x modules/social_features/social_event/social_event.module \_social_event_format_date()
- 10.1.x modules/social_features/social_event/social_event.module \_social_event_format_date()
- 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 246 - 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();
// 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"));
$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"));
// 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)) {
// Date are the same or there are no end date.
if (empty($end_datetime) || $start_datetime == $end_datetime) {
$date = $date_formatter
->format($start_datetime, $date_format);
$time = $date_formatter
->format($start_datetime, $time_format);
$event_date = "{$date} - {$time}";
}
elseif (date('Y-m-d', $start_datetime) == date('Y-m-d', $end_datetime)) {
$date = $date_formatter
->format($start_datetime, $date_format);
$start_time = $date_formatter
->format($start_datetime, $time_format);
$end_time = $date_formatter
->format($end_datetime, $time_format);
$event_date = "{$date} {$start_time} - {$end_time}";
}
elseif (!empty($end_datetime)) {
$start_date = $date_formatter
->format($start_datetime, $date_format);
$end_date = $date_formatter
->format($end_datetime, $date_format);
$start_time = $date_formatter
->format($start_datetime, $time_format);
$end_time = $date_formatter
->format($end_datetime, $time_format);
$event_date = "{$start_date} {$start_time} - {$end_date} {$end_time}";
}
}
return $event_date;
}