Displaying a date interval with Twig
Published on 2023-04-20 • Modified on 2023-04-20
This snippet shows how to display a date interval with Twig. In fact, we can use the diff()
function of the DateTime()
class and then format the result, a DateInterval
object, with the date
function.
{% set date1 = date('2023-04-20 22:16:16') %}
{% set date2 = date('2023-04-20 22:18:45') %}
Difference in minutes and seconds : <b>{{ date2.diff(date1)|date("%i'%s''") }}</b>
HTML demo of the snippet
Difference in minutes and seconds : 2'29''
More on Stackoverflow Read the doc More on the web 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! 😉
