How can I include a simple php in a Drupal 8 twig file? I'm making a custom theme, and I just want to include this in my twig file.

&lt;?php echo date("l H:i"); ?&gt;


Do I need make a twig extension? How can I do it?
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(&amp;$variables) {
  $variables['your_variable'] = \Drupal::get('date.formatter')-&gt;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