在Twig中显示DateInterval

时间:2017-01-20 09:43:21

标签: date twig symfony dateinterval

我尝试使用以下代码在Twig中显示DateInterval:

{{ event.endTime.diff(event.startTime)|date("i's''") }}

其中event是获得2个DateTime对象的实体:endTimestartTime。通过该命令,我得到了这个输出:

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 Extensionstime_diff,但这会返回一个字符串(inago)而不是Date对象,然后我可以&#39 ; t显示它我想要的。

如果您需要更多信息,请告诉我。谢谢你的帮助。

1 个答案:

答案 0 :(得分:6)

正如@Yoshi所说:

  

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetData(); } } public void GetData() { string SQL=string.Empty; SqlDataAdapter commandToBeLog=null; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString); SQL = " SELECT * FROM tbl_Video"; if (txtSearchVideo.Text.Trim() != string.Empty) { SQL += " WHERE VideoName LIKE @VideoName "; ListView1.DataSource = null; } SQL += " ORDER BY VideoID DESC"; DataSet ds = new DataSet(); SqlDataAdapter myCommand = new SqlDataAdapter( SQL, con); if (txtSearchVideo.Text.Trim() != string.Empty) { myCommand.SelectCommand.Parameters.Add("@VideoName", SqlDbType.NVarChar, 300).Value = "%" + txtSearchVideo.Text + "%"; } myCommand.Fill(ds); ListView1.DataSource = ds; ListView1.DataBind(); } protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); this.GetData(); } protected void btnSerch_Click(object sender, EventArgs e) { if (txtSearchVideo.Text.Trim() != string.Empty) { GetData(); GetData(); } else { GetData(); } } 也提供了(new \DateTime('tomorrow'))->diff(new \DateTime('now'))->format("i's''"),所以我说这不是一件小事。从手册:

     

"格式参数中识别以下字符   串。每个格式字符必须以百分号(%)为前缀。"

所以要解决我的问题,我必须这样做:

"i's''"