2

How can I check if today is the second or last sunday of this month? My effort:

{% set now = now|date('U') %}
{% set monthyear = now|date('F Y') %}
{% set secondSunday = now|date_modify("#{monthyear} second sunday")|date('U') %}
{% set lastSunday = now|date_modify("#{monthyear} last sunday")|date('U') %}

{# check if now is second or last sunday #}                    
{% if now == secondSunday or now == lastSunday %}
   {# YES, today is the second or the last sunday #}
{% else %}
   {# NOPE #}
{% endif %

But I keep getting the following error:

DateTime::modify(): Failed to parse time string (juni 2017 second sunday) at position 0 (j): The timezone could not be found in the database

Maybe it doesn't work because the monthyear variable is formed in dutch and not english? (juni = june) If this is the case, how can I modify this to english in order to get it to work?

Also, my timezone is correctly set to UTC+2 (CEST) - Europe/Brussels via Settings > General

Thanks!

mdmngz
  • 157
  • 6

1 Answers1

3

Maybe it doesn't work because the monthyear variable is formed in dutch and not english?

Yes that's part of the problem. Have a look at PHP's relative formats page to see what it supports.

You can actually pull off what you're doing with the this keyword.

{% set secondSunday = now|date_modify("second sunday of this month")%}
{% set lastSunday = now|date_modify("last sunday of this month") %}

You also don't need to define (or override) now since Craft will always set that to a DateTime object for you.

RitterKnight
  • 6,582
  • 14
  • 24