3

I would like to display current date and time to the header section.

I am using a subtheme of Bartik. Any suggestions?

3 Answers 3

5

You can also add code page.html.twig as well.

For print only date use {{ "now"|date("m/d/Y") }}

More Details

http://twig.sensiolabs.org/doc/2.x/filters/date.html

https://stackoverflow.com/questions/17364206/symfony2-how-can-i-set-twig-dated-f-y-filter-to-output-months-in-swedish

1
  • Does this take into account site/user TZ settings, localization, and handle cache contexts properly?
    – mpdonadio
    Commented Feb 9, 2017 at 14:49
2

I can't comment directly on Smalution's answer, but can confirm that

{{ "now"|date("m/d/Y") }}

in twig DOES respect drupal user timezones. (Tested it in america/new york - 2:50, switched to america/edmonton - 12:50) as per mpdonadio's question.

1
  • This should not be an answer, it should be a comment on the DRUPWAY´s answer.
    – ipwa
    Commented Dec 30, 2019 at 15:41
-1

For this you can create custom block and insert the PHP date function in it. (Install PHP module for insert php code in block or drupal site). Place this block on the header or where you want to place.

enter image description here

2
  • Does this take into account site/user TZ settings, localization, and handle cache contexts properly?
    – mpdonadio
    Commented Feb 9, 2017 at 14:51
  • 1
    Also not advised to inject PHP code as body content.
    – Kevin
    Commented Feb 9, 2017 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.