Symfony2 DateTime差异不行 [英] Symfony2 DateTime difference not working

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

问题描述

我的树枝上有这个var:

I have this var in my twig templete:

{{aWorker.timeToRetirement|date('Y-m-d')}}

当我执行页面时,它正好返回这个文本:'Ym-d'without''当然。现在我真正想要得到的是两个日期之间的区别,让我们说一个未来的日期(一个人/工作者的退休日期,你可以得到更多的信息,如果你需要,我在做什么 here ) '2060-12-01'和今天的日期'2013-10-03'。我想在我的实体类中使用这个var和方法:

when I execute the page it's returning exactly this text: 'Y-m-d' without '' of course. Now what I really want to get is the difference between 2 dates, lets say a future date (is the retirement date of a person/worker you can get more info about what I'm doing if u need it here) '2060-12-01' and today's date '2013-10-03'. I want to do that using this var and method in my Entity class:

//...
//Var containing the retirement date
private $retireYear;
//var to store time to retirement from today
private $timeToRetirement;
//...
    public function getTimeToRetirement (){
        $this->timeToRetirement = $this->retireYear->diff(new \DateTime('today'));
    return $this->timeToRetirement;
}

我做错了什么?这是var没有返回今天和退休日期之间的差异

What am I doing wrong? whay is the var not returning the difference between today and the retirement date

推荐答案

你的函数返回一个 DateInterval 对象,所以你需要使用它的 format()方法来生成所需的字符串。

Your function is returning a DateInterval object, so you need to use its format() method to generate your required string.

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

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