PHP Symfony2添加60年到两天的Datetime对象的一个人 [英] PHP Symfony2 add 60 years to the both day Datetime object of a person
问题描述
我需要在个人/工作人员的出生日期添加60年,这个人被存储在数据库中,我有以下代码来定义这个人加上getter和setter:
I need to add 60 Years to the date of birth of a person/worker, this person is stored in the database and I have the following code to define this person plus the getters and setters:
/**
* @ORM\Entity
* @ORM\Table(name="Worker")
* @ORM\Entity(repositoryClass="Osd\RetireBundle\Entity\WorkerRepository")
*/
class Worker
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $idWorker;
/**
* @ORM\Column(type="string", length=20)
*/
private $omang;
/**
* @ORM\Column(type="string", length=100)
*/
private $workerTitle;
/**
* @ORM\Column(type="string", length=100)
*/
private $workerName;
/**
* @ORM\Column(type="string", length=100)
*/
private $workerSurname;
/**
* @ORM\Column(type="date")
*/
private $birthDay;
/**
* @ORM\Column(type="date")
*/
private $dateOfEmployment;
在这个人/工作者里面,我真的不知道在这里做的是正确的,但是我试图创建一个这样的方法:
Inside this person/Worker, I do not really know is is correct to do it there, but I was trying to create a method like this:
public function getRetireDate (){
if($this->getBirthDay())
return $retireYear = $this->getBirthDay()->add(new DateInterval('P60Y'));
}
我真的不知道在哪里添加方法,但现在是在工人(Im新到symfony)。但是当我打电话给它时,它会给我这个错误:
I dont really know where to add the method?, but for now is inside the worker (Im new to symfony). but when ever I call it it gives me this error:
FatalErrorException: Error: Class 'Osd\RetireBundle\Entity\DateInterval' not found in /var/www/Org/src/Osd/RetireBundle/Entity/Worker.php line 202
谁做过或知道如何和在哪里实现这个功能的人?
提前感谢你。
Any one who has done or knows how and where to achieve this feature? Regards and thank you in advance.
推荐答案
我想我已经解决了问题,你的答案@david真的很有帮助;现在我的方法看起来像这样:
I think I have sorted out the problem and your answer @david was really helpful; now my method looks like this:
public function getRetireYear (){
return $this->retireYear = $this->getBirthDay()->add(new \DateInterval('P60Y'));
}
我在工作中有一个私有的var称为$ retireYear。而已。它奇妙地工作
谢谢。
and I have a private var inside the Worker called $retireYear. that's it. It works wonderfully. Thank you.
这篇关于PHP Symfony2添加60年到两天的Datetime对象的一个人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!