gpt4 book ai didi

php - Twig – 以前的格式

转载 作者:可可西里 更新时间:2023-10-31 22:18:06 26 4
gpt4 key购买 nike

我是 Twig 的新手,我希望将 datetime 格式转换为 time ago ,例如 2 hours ago 或 < strong>3 天前。有一个我一直在客户端使用的 jquery 插件 (jquery-timeago),但如果我能用 twig 做到这一点那就太好了。如果 twig 不附带此过滤器格式,是否有我可以使用的扩展?

最佳答案

Twig 的日期扩展完全符合您的要求:

{{ post.published_at|time_diff }}

The example above will output a string like 4 seconds ago or in 1 month, depending on the filtered date.

参见 http://twig.sensiolabs.org/doc/extensions/date.html (不再有效)工作链接 http://twig-extensions.readthedocs.io/en/latest/date.html

要完成这项工作,请按照下列步骤操作:

composer require twig/extensions

在使用 symfony 时将其添加到您的 services.yml

services:
twig.extension.date:
class: Twig_Extensions_Extension_Date
tags:
- { name: twig.extension }

关于php - Twig – 以前的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26311197/

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