Skip to content

Instantly share code, notes, and snippets.

@olets
Created January 6, 2020 21:33
Show Gist options
  • Save olets/1004293ede5b4e7b9af05fb1e76d9d6d to your computer and use it in GitHub Desktop.
Save olets/1004293ede5b4e7b9af05fb1e76d9d6d to your computer and use it in GitHub Desktop.
Twig: Determine if a date is past, present, or future
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