2

I want to check if it's winter. What is the best way to write a "between dates" conditional?

{% set saison = now|date('m-d') %}
{% if saison (saison >= '11-22' and saison <= '03-21') %}
  Jep it's winter
{% endif %}

This absolutely does not work. I also tried somthing like:

{% set saison = now|date('m-d') %}
{% if saison > '03-21' %}
  Now it's Spring
{% elseif saison > '06-21' %}
  Now it's Summer
{% elseif saison > '09-23' %}
  Now it's Fall
{% elseif saison > '12-22' %}
  Now it's Winter
{% endif %}

That doesn't work. I guess because twig doesn't recognize the strings as dates? How do I have to write this?

4

This conditional should work:

{# Saison Trigger #}
{% set saison = now|date('m-d') %}

{# Check for each Season #}
{% if saison > '11-22' or saison <= '03-21' %}
    {% set currentSaison = 'winter' %}
{% elseif saison > '03-21' and saison <= '06-21' %}
    {% set currentSaison = 'fruehling' %}
{% elseif saison > '06-21' and saison <= '09-22' %}
    {% set currentSaison = 'sommer' %}
{% elseif saison > '09-22' and saison <= '11-22' %}
    {% set currentSaison = 'herbst' %}
{% endif %}

You're right, Twig does just compare the strings. But this is totally fine in this case. A string representing a date and formatted like so "01-13" (don't forget the leading zero) works nicely with the comparison operators.

0

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.