Bug in Twig date between 2020 and 2021 | WordPress

112 Views Asked by At

I am using Timber/Twig and WordPress.

I have an Event Calendar created, and working, as well as a page displaying a full list of Events.

Something is buggy with my if statement now.

Only events from 2020/2021 between October and December show up.

Maybe someone has insight?

I've tested a few things and can't figure it out.

For clarification, this is setup so that if the date has passed, the event automatically doesn't show up.

{% if post.end_date|date('m-d-Y') >= now|date('m-d-Y') %}
  ...
{% endif %}

Thanks in advance.

2

There are 2 best solutions below

0
On

You should format date to "Y-m-d" format.

If post.end_date is in 2021/01/01. Formatting into "m-d-Y" string results into "01-01-2021". Date now in "m-d-Y" format is "10-05-2020".

Comparing strings:

("01-01-2021" >= "10-05-2020") // result: false
0
On

If anyone has this issue this fixed it for me:

date(post.end_date) >= date(now)