Skip to content

Instantly share code, notes, and snippets.

@gwagroves
Last active March 13, 2018 10:47
Show Gist options
  • Save gwagroves/625b369b75f6c3d640abf2191e091ff5 to your computer and use it in GitHub Desktop.
Save gwagroves/625b369b75f6c3d640abf2191e091ff5 to your computer and use it in GitHub Desktop.
Drupal 8 calculate separate time for date time field
<?php
/**
* Implements hook_preprocess_HOOK().
*/
function module_preprocess_node__content_type(&$variables) {
$node = $variables['elements']['#node'];
// Get time as separate variable.
// Calculate using system timezone.
if (!$node->field_my_datetime->isEmpty()) {
$date_string = $node->field_my_datetime->value;
$timezone = \Drupal::config('system.date')->get('timezone.default');
$mydate = new DateTime($date_string, new DateTimeZone('UTC'));
$mydate->setTimezone(new DateTimeZone($timezone));
$variables['time_only'] = $mydate->format('H:i');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment