1

I have this var in my twig templete:

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

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;
}

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

Community
  • 1
  • 1
Abel
  • 538
  • 5
  • 8
  • 28

1 Answers1

2

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

vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • Totally right, thank you @vascowhite, I have changed the return to: `code return $this->timeToRetirement->format('%Y years, %m months, %d days');` and the template var to: `code {{aWorker.timeToRetirement}}` Thank you again. I don know how to put code on my coments the – Abel Oct 03 '13 at 09:57
  • Sorry about the typos, I'm learning how to do the comments now. – Abel Oct 03 '13 at 10:03