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

5

14

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?

Polysaccharide answered 26/12, 2016 at 16:23 Comment(0)
S
28

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 %}
Subsellium answered 26/12, 2016 at 21:10 Comment(3)
Actually I changed the comparison signs to ` {% if date(room.price_start_date) < date() and date(room.price_end_date) > date() %} ` and worked. Many Thanks !Polysaccharide
@AhmedEssam, thanks, you are right, I updated the comparison signs the right way. Feel free to accept the answer, and hope it helps to someone else as well!Subsellium
This will only work if both start and end are set. If any of those are "open" or null wont work correctly.Androcles
H
2

Use \DateTime instances to compare dates in Twig (as well as PHP).

What is wrong?

date('Y-m-d') function returns a formatted date string.

So, you should to change it to $today = new \DateTime('today'); and pass this instance to Twig template or use date() Twig function directly in your condition.

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

Assuming that these two (room.price_start_date and room.price_end_date) are instances of \DateTime, then your Twig code should work fine.

Hajj answered 26/12, 2016 at 20:59 Comment(1)
It should be noted that YYYY-MM-DD date strings (date('Y-m-d')) are correctly comparable in PHP 3v4l.org/bCfZv and twig twigfiddle.com/opjcw8Gump
D
0

Try the date function: http://twig.sensiolabs.org/doc/functions/date.html

You might have to change your code a bit, so I can't advise best.

EDIT #2

Sorry I didn't have a chance to look over the docs on the Twig date function, but it's good that @Yonel and @Farside did. @Yonel's points are the ones that I was concerned with, and didn't have time to give a good answer.

If you are using \DateTime, then this would be your code changes:

{% if date() < date(room.price_start_date) and date() > date(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 %}

I improved the formatting too. I presume you are just checking that the user can click book a room within that timeframe. And your code looks good.

You could add {{ dump(room.price_start_date) }} and so on in the DEV environment to debug what your actual dates look like as a troubleshooting technique.

Dudeen answered 26/12, 2016 at 17:41 Comment(0)
A
0

For open dates solution consider extending @Farside solution a bit as follows:

    {% if ( date(room.price_start_date) is null or date(room.price_start_date) < date())  and   
          ( date(room.price_end_date)   is null or date(room.price_end_date)   > date()) %}

      {# condition met #}

    {% endif %}

Also take into account that if neither start or end is set it will consider everything inside the range.

Androcles answered 19/2, 2020 at 9:27 Comment(0)
F
0

To simplify, use this:

{% if start_date <= check_date and check_date <= end_date %}<br>
     ... your code<br>
{% else %}<br>
     ... your code<br>
{% endif %}
Freedman answered 12/1, 2023 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.