how to check if today is between two dates in Twig?
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 %}
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
andprice_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.