Add today's date

As D8 no longer allows/desires PHP to be injected into, well, anything, can anyone help me with how to display in realtime today’s (now) date and time? It’s in principle nothing to do with the node itself: I just need a ‘now’ timestamp display so people know the graphs they are seeing are realtime.

I was expecting to use a block, but it can go into a node page also… how do I do this? Do I need a specific text format?

I think the right token is {{ “now”|date(“d/m/Y”) }} but I have no idea how to make that render into anything.


the token you posted is a twig variable, so it must be placed in the theme, in case you can modify it.

The other way to have that into your website is by implementing a module that will expose a new block to print that.
Seems pretty complicated for this use case.

Hi Eduardo - thanks for the tips.

I’ve in the meantime created a custom block to do this, but the issue now is that the date/time field only displays the date/time the block was created (I set the default value to ‘now’ thinking that would solve it). It doesn’t update at all.

I can’t figure out how to get the PHP in, if I have to go down that route. None of the text formats will allow PHP to be processed, so I’m rather stuck.


Hello Cath,

What you actually need to do is to render a date inside a block. This used to be “easy” with Drupal 7 by injecting PHP code inside your block. However this logic is not safe and thats why it is not possible in D8.

The D8 way to do something like is by using twig. Twig is a PHP template engine that has replaced php injection and templating in Drupal 8. In fact, the code that you wrote in your question [ {{ “now”|date(“d/m/Y”) }} ] is written using twig. In general, All the templates in D8 are written using twig. The good thing with twig is that it permits to render elements like the date using predefined functions ( in your case it is the date() function.

In our topic. There is at least one way that you can achieve what you want:

In order to achieve it you need to add a field in your Block that permits you to add twig.

But how can I add a field in my block? In Drupal 8 there is the concept of block types which gives you the opportunity to create scaffolds of blocks, just like you do with content types. If you visit /admin/structure/block/block-content/types you can add/edit your block types. In this way you can add a new field.
And how can I add a field that renders twig? The Twig field module provides this functionality.

In total:

  1. Install the Twig field module
  2. Add a field of type Twig in one of your block types (or create a new block type with this field type)
  3. Create a block and inside the twig field add the custom twig code {{ "now"|date("m/d/Y") }}
  4. Place your new block in your website by using the Block Layout admin page

Hope this helped. Let me know if you need more details on that.


1 Like

Hi Kostas,

Thanks for that - I would just add that one needs to also install the CodeMirror module to use Twig field module.

Date is now showing :slight_smile: I also needed time, so I put the following in:

{{ "now"|date("l d F Y, H:m") }}

Unfortunately it’s still not really working, in that the time is static - that of when I created that block - 15:01 (plus an hour due to something in the timezone somewhere). Have a look at which is where it will be displayed.

I tried ticking ‘Twig’ in the CodeMirror configuration to see if that did anything, but nothing happened. I don’t know what to do now.

Any ideas? I’m working on if you’d like to have a go…


@noble I changed your block to display {{ "now"|date("m/d/Y H:h") }} and as you can see in the block actually renders and shows the current date and time.

However keep in mind that with this implementatio it will show the time of the moment that your page refreshed. So if you stay in the page for 10 mins without refreshing, the block will show the time from 10 mins ago.

Edit: I just noticed that actually shows the time that it was created, as you mentioned before. I will come up with another solution.



After doing a small research I realized that in the end the Date Format was incorrect. Twig follows the PHP date format, so it seems that we were both wrong with the format.

You can use the link above in order to find the date format that you prefer. Furthermore, I just noticed that the block is cached which also creates an issue. If you clear the caches you will notice that the time changes when you refresh the page.


Hi Kostas,

Thanks for looking into this.

I’m still not sure what to actually put as the complete string, though. If it’s not the string we were originally using, then what should I put?

I don’t have any knowledge of programming languages, so I’m not in a position to construct this kind of thing myself. While I can identify bits I need from the PHP manual (ie. in this case, I want “l d F Y, H:m” to give “Monday 28 January 2019, 14:00”), how do I actually encapsulate that to work in this Twig field?

If our original string we used is incorrect, then I can see the PHP manual suggests:

date ( string$format[, int$timestamp= time() ] ) : string

… but honestly I have no idea what I should edit there and where my formatting is supposed to go. Can you give me the correct string to use around my date format that I can just paste straight into that Twig field?

And next question - how do we stop blocks being cached? I set the “Browser and Proxy cache max age” to 1 minute, but it doesn’t seem to have made any difference. I can live with a < F5 > refresh, but that doesn’t work here. Having to manually clear caches every time to force the update doesn’t seem great. I’m having a similar problem with another website (Indico fetch module calendar block is not updating automatically unless you clear the caches) so it would be good to know if there’s a trick to this.

Thank you for your time and help - I know I’m asking a lot of questions here…!