树枝,减去2个日期 [英] Twig, subtract 2 dates

查看:34
本文介绍了树枝,减去2个日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用 twig 减去两个日期?

How can I subtract two dates with twig ?

我有一个结束日期和一个开始日期,我想要两者之间的区别.

I have an ending date and a begining date and I want the difference between both.

例如,对于日期2015-02-20"和2015-02-13",我应该获得7".

For example for both date "2015-02-20" and "2015-02-13" I should obtain "7".

我该怎么做?

感谢帮助

推荐答案

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

您可以使用 KnpTimeBundle

在树枝中:这与当前日期进行比较:

In the Twig: This compare with the current date:

{# Returns something like "3 minutes ago" #}
{{ time_diff(form) }}

这与另一个日期比较:

{# Returns something like "3 minutes ago" #}
{{ time_diff(form , to ) }}

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

通过php函数进行diff:

Make diff via php function:

$calcFrom = $from;

$calcTo = $to;
$now->diff($calcFrom)->format("%a")

并通过 Twig 扩展 或直接在实体中的辅助方法.

And make it available via a Twig extension or directly in an helper method in the entity.

另一种可能的解决方案是编写一个自定义注册DQL 函数在存储库中完成工作

Another possible solution is to write register a custom DQL Function to do the work in the repository

希望对您有帮助

这篇关于树枝,减去2个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆