Last active
March 13, 2018 10:47
-
-
Save gwagroves/625b369b75f6c3d640abf2191e091ff5 to your computer and use it in GitHub Desktop.
Drupal 8 calculate separate time for date time field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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