日期差异 [英] Twig date difference

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

问题描述

我有一个具有开始日期和结束日期的实体。

I've got an entity with a starting date and an ending date.

是否可以通过使用twig获得时间差异? p>

Is it possible to get the difference in time between them by using twig?

推荐答案

没有内置函数可以做到这一点,但你可以很容易地做到这一点, extend twig 很容易!

There's no built-in function to do that, but you can easily do it yourself, extending twig is easy!

快速的方式是使用twig的简单函数类来实现:

The quick'n'easy way is to do it with twig's simple function class:

$function = new Twig_SimpleFunction('date_difference', function ($start, $end) {
    // ... calculate difference and return it
});

// assuming $twig is an instance of Twig_Environment
$twig->addFunction($function);

可重用的方法是创建一个twig扩展(记录在同一个链接上)。这很简单。

The reusable way is to create a twig extension (documented on the same link). That's still easy.

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

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