3
{% set event_date = event.schedule|date('d-m-y') %}
{% set nowdate = "now"|date('d-m-y') %}
{% if event_date < nowdate %}
    view some 1
{% else %}
    view some 2
{% endif %}

If change event_date bigger or lower than nowdate, have one result: view some 2. Why doesn't work?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
chrisperk
  • 47
  • 8

3 Answers3

1

You're comparing strings. d-m-y is not a usable format for comparing dates. Try Y-m-d, where the parts are in order of importance.

Even better, you could compare two \DateTime objects instead of strings. I assume event.schedule is one, you just need to pass one for the current date to twig.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85
1

You are in a string comparison situation. For better approach this, use the U filter as follow:

{% set event_date = event.schedule|date('U') %}
{% set nowdate = "now"|date('U') %}
{% if event_date < nowdate %}
    view some 1
{% else %}
    view some 2
{% endif %}

{{ event_date }}

Live testing in this fiddle.

Hope this help

Matteo
  • 37,680
  • 11
  • 100
  • 115
0

In addition to the date filter, Twig defines a date() function which has been designed precisely to compare dates.

Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44