0

我有一个名为 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 并将其传递给视图,否则我必须去up 和 down 数组集合。

有关该问题的更多信息,您可以阅读此线程

4

2 回答 2

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 个更简单、更可重用的函数,如果我需要从这个环境之外创建帮助器,我可能会创建它。在控制器中,我访问配置文件:

         $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 %}

它工作正常......与违反哲学的独立性无关!?

于 2013-10-21T09:39:41.757 回答
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;
}

然后,您可以使用dateTwig 过滤器DateInterval在视图中格式化。请参阅http://twig.sensiolabs.org/doc/filters/date.html 如果您需要更具体或封装的内容,您还可以为此创建一个 Twig 扩展。请参阅http://symfony.com/doc/master/cookbook/template/twig_extension.html

于 2013-10-16T14:56:47.630 回答