我有一个名为worker的实体的日期间隔,有一段时间看下一个函数:
// Get date of retirement
public function getRetireYear()
{
$this->retireYear = $this->getBirthDay()->add(new \DateInterval('P60Y'));
return $this->retireYear;
}
// Get time left for retirement
public function getTimeToRetirement()
{
// Date of today
$today = new \DateTime('today');
// Calculate date diff between today and retirement day.
$this->timeToRetirement = $today->diff($this->retireYear);
// Return the time to retirement time formated
return $this->formatTimeInterval(
$this->timeToRetirement,
'%r%Y years, %m months, %d days'
);
}
//Format the dateInterval numbers as string
public function formatTimeInterval($aDateDif, $format)
{
return $aDateDif->format($format);
}
此处存储在数据库中的唯一值是$this->getBirthDay()
,然后getRetireYear
生成工人退休年份(birthDay + 60年)。
getTimeToRetirement
给出了退休某人的时间,formatTimeInterval($,$)
用于格式化日期间隔。
现在我需要能够以不同的格式使用这个dateInterval,例如有时我只需要将年份作为整数与视图中的其他值进行比较,所以我需要今年作为单个值才能在视图。
我尝试了一些事情,但我不知道正确的方法,所以我没有得到更好的结论。
有时我需要其他任务的月份和日期,而不仅仅是年份,这些值应该可以在视图上访问,因为在控制器中我只是搜索对象的ArrayColletion并将其传递给视图,否则我必须上升并降低阵列集合。
有关该问题的详细信息,请参阅this thread
答案 0 :(得分:1)
我认为最好让getTimeToRetirement
方法返回DateInterval
并将formatTimeInterval
方法完全移出实体,因为它与DateInterval
更相关格式化而不是你的工人。然后,您可以根据需要格式化DateInterval
。
这些方面的东西。
在您的工作组实体类中:
// Get date of retirement
public function getRetireYear()
{
$this->retireYear = $this->getBirthDay()->add(new \DateInterval('P60Y'));
return $this->retireYear;
}
// Get time left for retirement
public function getTimeToRetirement()
{
// Date of today
$today = new \DateTime('today');
// Calculate date diff between today and retirement day.
$this->timeToRetirement = $today->diff($this->retireYear);
// Return the time to retirement time as a DateInterval
return $this->timeToRetirement;
}
然后,您可以使用date
Twig过滤器格式化视图中的DateInterval
。见http://twig.sensiolabs.org/doc/filters/date.html
如果您需要更具体或封装的内容,您还可以为此创建一个Twig扩展。见http://symfony.com/doc/master/cookbook/templating/twig_extension.html
答案 1 :(得分:1)
这就是我所做的,但可能不是最实用的方式:
public function getYearsLeft(){
$aux = $this->birthDay->add(new \DateInterval('P60Y'));
//date today
$today = new \DateTime('today');
// Calculate date diff between today and retirement day.
$this->yearsLeft = $today->diff($aux);
$aux2 = $this->formatTimeInterval($this->yearsLeft, '%r%Y');
$this->birthDay->sub(new \DateInterval('P60Y'));
return $aux2;
}
public function getMonthsLetf(){
$aux = $this->birthDay->add(new \DateInterval('P60Y'));
//date today
$today = new \DateTime('today');
// Calculate date diff between today and retirement day.
$this->monthsLetf = $today->diff($aux);
//$this->yearsLeft = $aDateDif; //('%r%Y');
$aux2 = $this->formatTimeInterval($this->monthsLetf, '%m');
$this->birthDay->sub(new \DateInterval('P60Y'));
return $aux2;
}
我将在3个函数中转换这两个函数更简单,更可重用,如果我需要从这个环境外部创建帮助器。在Controller中我访问配置文件:
$redTime = $this->container->getParameter('time_red');
$orangeTime = $this->container->getParameter('time_orange');
当然,我不必将工作者列表传递给视图以及配置变量。现在在视图中我可以按如下方式访问变量:
{% for aWorker in aWorkerList %}
{% if aWorker.retireYear is defined %}
{% if colorOrange > aWorker.yearsLeft %}
<tr bgcolor="orange">
{% if colorRed > aWorker.monthsLetf %}
<tr bgcolor="red">
{% elseif 0 > aWorker.yearsLeft %}
<tr bgcolor="red">
{% endif %}
{% endif %}
<td><a href="{{path('osd_retire_editworkerpage', { 'id': aWorker.idWorker })}}">Edit</a>
<a href="{{path('osd_retire_deleteworkerpage', { 'id': aWorker.idWorker })}}">Delete</a></td>
<td>{{aWorker.omang}}</td>
<td>{{aWorker.workerTitle}}</td>
<td>{{aWorker.workerName}}</td>
<td>{{aWorker.workerSurname}}</td>
<td>{{aWorker.birthDay|date('Y-m-d')}}</td>
<td>{{aWorker.dateOfEmployment|date('Y-m-d')}}</td>
<td>{{aWorker.retireYear|date('Y-m-d')}}</td>
<td>{{aWorker.timeToRetirement}}</td>
<td>{{aWorker.fileNumber}}</td>
</tr>
{% endif %}
{% endfor %}
它的工作正常......违反哲学的独立性!?