我正在尝试在Symfony上使用Twig进行比较,以显示具有不同样式的文本,但结果却很奇怪。该值来自我的实体(上下文)函数:
public function getDaysToExpire(): int
{
$creationDate = $this->created_at;
$duration = $this->duration;
$finalDate = $creationDate->modify('+' . $duration . ' days');
$currentDate = new \DateTime();
$difference = $currentDate->diff($finalDate);
return $difference->days;
}
它正在按预期方式工作,并作为结果返回一个整数。
在我的树枝上,我有:
{% if context.getDaysToExpire > 5 %}
<p class="context-days">Jours restants : {{ context.getDaysToExpire }}</p>
{% else %}
<p class="context-days">Jours restants : <span class="bolder">{{ context.getDaysToExpire }}</span></p>
{% endif %}
如果不使用if子句,则会从context.getDaysToExpire获得正确的值。但是,使用此代码,我得到一个奇怪的结果:第一个条件为+ 30,第二个条件为+随机值。
我做错了什么?
这应该工作
public function getDaysToExpire(): int
{
$creationDate = clone $this->created_at; // prevent modification on $this->created_at
$duration = $this->duration;
$finalDate = $creationDate->modify('+' . $duration . ' days');
$currentDate = new \DateTime();
$difference = $currentDate->diff($finalDate);
return $difference->days;
}