13

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?

Ahmed Essam
  • 1,000
  • 2
  • 8
  • 23

5 Answers5

27

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 %}
Farside
  • 9,923
  • 4
  • 47
  • 60
  • 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 ! – Ahmed Essam Dec 27 '16 at 09:01
  • @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! – Farside Dec 27 '16 at 14:45
  • 1
    This will only work if both start and end are set. If any of those are "open" or null wont work correctly. – Mbotet Feb 19 '20 at 09:17
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.

yceruto
  • 9,230
  • 5
  • 38
  • 65
  • It should be noted that `YYYY-MM-DD` date strings (`date('Y-m-d')`) are correctly comparable in PHP https://3v4l.org/bCfZv and twig https://twigfiddle.com/opjcw8 – Will B. Sep 28 '18 at 03:42
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.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
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.

Mbotet
  • 170
  • 2
  • 17
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 %}
Fedor
  • 17,146
  • 13
  • 40
  • 131