Created
January 6, 2020 21:33
-
-
Save olets/1004293ede5b4e7b9af05fb1e76d9d6d to your computer and use it in GitHub Desktop.
Twig: Determine if a date is past, present, or future
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://twigfiddle.com/ujg6hr | |
{% macro when(theDate = 'now', precision = 'd F Y') %} | |
{% set difference = 'now'|date(precision)|date('U') - theDate|date(precision)|date('U') %} | |
{{ difference == 0 ? 'now' : (difference > 0 ? 'past' : 'future') }} | |
{% endmacro %} | |
{% set currentDay = 'now'|date('d') %} | |
{% set currentMonth = 'now'|date('F') %} | |
{% set currentYear = 'now'|date('Y') %} | |
{{ _self.when("#{currentDay} #{currentMonth} #{currentYear - 1}") }} | |
{{ _self.when("#{currentDay} #{currentMonth} #{currentYear}") }} | |
{{ _self.when("#{currentDay - 1 > 0 ? currentDay - 1 : currentDay + 1} #{currentMonth} #{currentYear}", | |
'Y') }} | |
{{ _self.when("#{currentDay} #{currentMonth} #{currentYear + 1}") }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment