date
将参数转换为日期以允许日期比较
1 2 3
{% if date(user.created_at) < date('-2days') %}
{# do something #}
{% endif %}
参数必须是 PHP 支持的日期和时间格式之一。
您可以将时区作为第二个参数传递
1 2 3
{% if date(user.created_at) < date('-2days', 'Europe/Paris') %}
{# do something #}
{% endif %}
如果未传递任何参数,该函数将返回当前日期
1 2 3
{% if date(user.created_at) < date() %}
{# always! #}
{% endif %}
注意
您可以通过在 core
扩展实例上调用 setTimezone()
来全局设置默认时区
1 2
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
参数
date
:日期timezone
:时区