I'm making a custom theme, and I just want to include this in my twig file.

<?php echo date("l H:i"); ?>

Do I need make a twig extension? How can I do it?

  • 1
    @tecmoc I'm not sure why your question was closed, but Twig has a built in date filter. Apparently not everyone is up to speed on drupal 8. Anyhow, something like this should work for ya, {{ "now"|date("l H:i") }} Welcome to drupal.stackexchange!
    – Chance G
    Feb 10, 2016 at 23:53
  • After talking with the other moderators, I re-opened the question. There are details that are specific to Drupal and that need to be considered in a correct answer. It is not as simple as using a Twig filter.
    – apaderno
    Feb 11, 2016 at 19:58
  • Thank you very much, I don´t want to be annoying, really, I appreciate your time, from all users, and I'm sure the answers will help me a lot. I'm new in Drupal and twig and I have many doubts. Thanks again.
    – tecmoc
    Feb 12, 2016 at 2:38

1 Answer 1


One way to do it would be to add the variable in a preprocess function, and print that out in the chosen twig template as {{ your_variable }}.

function yourtheme_preprocess_page(&$variables) {
  $variables['your_variable'] = \Drupal::get('date.formatter')->format(args);

I'm not sure where you are doing this, so your use case could be _page, _node, _html, _block for the preprocessor.

Since you are not passing a timestamp to date, it will assume current time and print that. If that is your intent, you should consider that when caching mechanisms are enabled, it won't show the current time anymore, and you should consider a Javascript based solution to keep that accurate (something like momentjs?). Again, don't know your full use case here.

edit: see https://api.drupal.org/api/drupal/core%21includes%21common.inc/function/format_date/8

and https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%21DateFormatter.php/class/DateFormatter/8

  • There are a few things wrong with this. One, you should really be using the date formatter service. In a preprocess, you can get it with \Drupal::get('date.formatter'), and then use the methods on that service (usually just ->format(). This lets you use your predefined formats, and also handle localization properly. Two, since your output now is dependent on the date, you need to set a max age (probably 0) to $variables['#cache']. See drupal.org/developing/api/8/render/arrays/cacheability for more info.
    – mpdonadio
    Feb 11, 2016 at 23:04
  • Continued ... MomentJS isn't a total solution solution for this, but it may end up in core in 8.2 as part of some other work planned for the datetime module.
    – mpdonadio
    Feb 11, 2016 at 23:12
  • Just using it as an example, I am not totally up on all things D8 yet.
    – Kevin
    Feb 12, 2016 at 1:53

Your Answer

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

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