【发布时间】:2017-06-05 06:03:46
【问题描述】:
我正在尝试使用以下代码在 Twig 中显示 DateInterval:
{{ event.endTime.diff(event.startTime)|date("i's''") }}
其中event 是一个获得2 个DateTime 对象的实体:endTime 和startTime。使用该命令,我得到了以下输出:
i's''
而不是min'sec'' 像08'15''
date doc里说
日期过滤器接受 [...] DateInterval 实例
这项工作用于显示日期对象的分钟和秒。
请注意,这样做:{{ (event.endTime.diff(event.startTime))|date("i's''") }} 不会改变任何东西
我也试过{{ date(event.endTime.diff(event.startTime))|date("i's''") }},但这会导致异常Object of class DateInterval could not be converted to string
我还从Twig Extensions 看到了time_diff,但这会返回一个字符串(in 或ago)而不是 Date 对象,然后我无法按我的意愿显示它。
如果您需要更多信息,请告诉我。感谢您的帮助。
【问题讨论】:
-
(new \DateTime('tomorrow'))->diff(new \DateTime('now'))->format("i's''")也给出了"i's''",所以我想说这不是twig-thing。来自the manual:“在格式参数字符串中可以识别以下字符。每个格式字符必须以百分号 (%) 为前缀。” -
非常感谢,事实上我必须在
i和s之前加上%
标签: date twig symfony dateinterval