symfonytwigoctobercms

how to check if today is between two dates in Twig?


I'd like to check if today's date is between two dates from the database. Here's my code.

{% if today < room.price_start_date and today > room.price_end_date %}
<a href="{{'/'|app}}/book/{{room.id}}"><button type="button" class="btn btn-default btn-xs">Book this room</button></a>
{% else %}
<a href="{{'/'|app}}/contact"><button type="button" class="btn btn-default btn-xs">Book this room</button></a>
{% endif %}

The today variable gets its value from this code:

$todayDate = date('Y-m-d');
$this['today'] = date('Y-m-d', strtotime($todayDate));

The price_start_date and price_end_date I get them from database and their columns' type is Date

Any idea how to check if today is between room.price_start_date and room.price_end_date in Twig?


Solution

  • According to the TWIG manual, you can use date function. If no argument is passed, the function returns the current date.

    So your code might look like this in TWIG:

    {% if date(room.price_start_date) < date() and date(room.price_end_date) > date() %}
      {# condition met #}
    {% endif %}