如何检查今天是否在Twig的两个日期之间?

Ahm*_*sam 10 symfony twig octobercms

我想检查今天的日期是否在数据库的两个日期之间.这是我的代码.

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

today变量会从这段代码的价值:

$todayDate = date('Y-m-d');
$this['today'] = date('Y-m-d', strtotime($todayDate));
Run Code Online (Sandbox Code Playgroud)

price_start_dateprice_end_date我从数据库中获取他们和他们的列的类型Date

任何想法如何检查是否today是之间room.price_start_dateroom.price_end_date在嫩枝?

Far*_*ide 14

根据TWIG手册,您可以使用date功能.如果没有传递参数,则该函数返回当前日期.

所以你的代码可能在TWIG中看起来像这样:

{% if date(room.price_start_date) < date() and date(room.price_end_date) > date() %}
  {# condition met #}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

  • 实际上我把比较标志改为`{%if date(room.price_start_date)<date()和date(room.price_end_date)> date()%}`并且工作了.非常感谢 ! (3认同)