I create a field with date format for my content and I set date and time:

2017-12-23 21:45:36

Now when I want to print with these codes:

function mytheme_preprocess_node(&$variables) {
    $date = $variables['node']->get('myfieldname')->value;
    $variables['date'] = $date ;


{% set date = node.myfieldname.value %}

and use {{ date }} in my template, print:


( 4:30 minutes difference )

How can I fix it in Drupal 8?

  • 1
    That seems to be a timezone issue. Check your website and server timezones. – Neograph734 Sep 4 '17 at 8:53
  • I use it in localhost and time is set correctly – RobinSon Sep 4 '17 at 8:54
  • I do not know your experience level, but just because your computer time is right does not automatically mean your webserver timezone is right. The php.ini file can define a timezone as well, offsetting the time from your system time. – Neograph734 Sep 4 '17 at 9:06
  • I try but not work. For what I set the time but in print change !! – RobinSon Sep 4 '17 at 9:15
  • This is a timezone issue, but not a configuration issue. It's about how the date is stored in the database. The date field is stored in UTC and if you get the raw value you get UTC. Use the computed property ->date instead, which has methods to process the date, either getTimeStamp() for the Unix timestamp to use php date functions or format() for printing a formatted date. – 4k4 Sep 4 '17 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.