Escape reserved characters when using the Twig date filter
Published on 2019-08-14 • Modified on 2019-08-14
Sometimes when using the PHP date function you need to use characters how are usually reserved to be converted in some dynamic values. To prevent PHP from converting these ones you need to escape them. When using Twig you must use a double anti-slash when calling the date
filter: \\
{{ 'now'|date('\\Y: Y, \\M: M, \\d: d') }}
Code execution output:
Y: 2025, M: Mar, d: 17
More on Stackoverflow Read the doc Random snippet
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉
