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!

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.

1
  • 1
    Awesome, thank you very much!
    – mdmngz
    Jun 6 '17 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.