Dirk J. Faber
Dirk J. Faber

Reputation: 4691

Twig date and time formatting with text in between

In Twig I want to show a DateTime variable like this:

2018-10-22 at 15:03:18

I tried this: {{ dateTimeVariable |date('Y-m-d at H:i:s') }}, but this returns

2018-10-22 pm31 15:03:18

The result makes sense because the a from at returns the lowercase am or pm and t the numbers in the month. For more on this, see the date formatting guide.

How though, can I get some text in between the date and the time? Do I have to use the same variable twice, once for date and one for time with the text in between or is there a better solution?

Upvotes: 2

Views: 930

Answers (2)

Michał G
Michał G

Reputation: 2292

try

{{ dateTimeVariable |date('Y-m-d') }} at {{ dateTimeVariable |date('H:i:s') }}

Upvotes: 2

Dirk J. Faber
Dirk J. Faber

Reputation: 4691

Escaping words and characters in Twig has to be done with a double backslah \\, therefore what works is:

{{ dateTimeVariable |date('Y-m-d \\a\\t H:i:s') }}

For more information see the Twig Documentation.

Upvotes: 3

Related Questions