gpt4 book ai didi

php - Symfony2 DATEDIFF 函数

转载 作者:可可西里 更新时间:2023-11-01 01:00:59 25 4
gpt4 key购买 nike

我需要用数据库中的数据填充 TWIG 中的表格。一切都很好,除了这个:

我需要具有 DATEDIFF 属性的列来获取天数。

TODAY-dateFromDateBase

问题是:如何获取在 twig 中循环的天数?

这是我的 Twig :

<table>
<thead>
<tr>
<form action="" method="post" {{ form_enctype(searchform) }} class="form-index-permits">
<td>L.p </td>
<td>ID PRZEPUSTKI {{ form_widget(searchform.PermitId) }}</td>
<td>Name{{ form_widget(searchform.Permitname) }}</td>
<td>Surname {{ form_widget(searchform.Permitsurname) }}</td>
<td>Company {{ form_widget(searchform.Company) }}</td>
<td>GW {{ form_widget(searchform.Contractor) }}</td>
<td>Dayleft {{ form_widget(searchform.Dayleft) }}</td>
<td>End date {{ form_widget(searchform.date, { 'attr': {'class': 'datepicker'} }) }}</td>

</form>
</tr>

</thead>
{% for permit in permitcollection %}
<tbody>
<td>{{ loop.index }}</td>
<td>{{ permit.getPermitid()|number_format(0, '.', ' ') }}</td>
<td>{{ permit.getPermitname() }}</td>
<td>{{ permit.getPermitsurname() }}</td>
<td>{{ permit.getPermitsCompany().getName() }}</td>
<td>{{ permit.getPermitsContractor().getName() }}</td>
<td> HERE I WANT TO DISPLAY DAYS LEFT</td>
<td>{{ permit.getExpirationdate()|date('Y-m-d') }}</td>

</tbody>
{% endfor %}
</table>

这样的事情可能吗?

{{ permit.getExpirationdate()|date('Y-m-d') - "now"|date('Y-m-d')  }}

最佳答案

第一个解决方案(推荐)“使用现有库”:

您可以使用 KnpTimeBundle

在 Twig 中:这与当前日期比较:

{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate) }}

这与另一个日期比较:

{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate, anotherDate) }}

第二种解决方案“自己动手”:

通过 php 函数进行比较:

$calcFrom = permit.getExpirationdate()

$now = new \DateTime('now');
$now->diff($calcFrom)->format("%a")

并通过 Twig extension 使其可用或直接在实体的辅助方法中。

另一种可能的解决方案 是写入寄存器 a custom DQL Function在存储库中完成工作

希望对你有帮助

关于php - Symfony2 DATEDIFF 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com