I have some variable named setting.graduate_start
and setting.graduate_end
. Both have MySQL DATETIME format like 2020-08-05 11:55:00
.
All I want is tp display a menu if today is between setting.graduate_start
and setting.graduate_end
.
So I wrote the code like this:
{% if date(setting.graduate_start) > date() and date(setting.graduate_end) < date()%}
<li><a href="/graduation">Graduation</a></li>
{% endif %}
setting.graduate_start
is 2020-08-05 11:55:00
and setting.graduate_end
is 2020-08-10 12:00:00
, so, when today is 2020-08-06
the list element is supposed to be displayed but it wasn't.
How can I fix it?
date()
, in Twig, is a filter to format date , alone it doesn't do anything .
If you want the current date, you'll need to use 'now' | date()
'now' | date()
, but that will still not give the format you expect.
But as prompted in the documentation:
The format specifier is the same as supported by date , except when the filtered data is of type DateInterval , when the format must conform to DateInterval::format instead.
Source: https://twig.symfony.com/doc/3.x/filters/date.html#date
So with all this together:
{% if 'now' | date('Y-m-d H:i:s') > setting.graduate_start | date('Y-m-d H:i:s') and 'now' | date('Y-m-d H:i:s') < setting.graduate_end | date('Y-m-d H:i:s') %}
<li><a href="/graduation">Graduation</a></li>
{% endif %}
Renders your link as it should .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.