Reputation: 31
I have given two times(doctrine type time) and want to check, if the current time is between these times in a for-if-loop. I use the Date compare from twig. My code looks like this:
{% for time in restaurant.openingHours if ((time.day == day) and (date(time.opening) <= date()) and (date(time.closing) >= date())) %}
<span class="restaurant_info green"></span>
{% else %}
<span class="restaurant_info red"></span>
{% endfor %}
The last condition(date(time.closing) >= date()
) is false but shut be true. Has anybody a idea to fix this problem?
Upvotes: 1
Views: 981
Reputation: 634
For oversight sake I would change your code and split the forloop and if statement. In order to check what is going wrong just dump the variables in the twig, like followed.
{%dump(time.opening)%}
{%dump(time.closing)%}
{%dump(date)%}
{% for time in restaurant.openingHours %}
{%if ((time.day == day) and (date(time.opening) <= date()) and (date(time.closing) >= date()))%}
<span class="restaurant_info green"></span>
{% else %}
<span class="restaurant_info red"></span>
{%endif%}
{% endfor %}
Upvotes: 2