具有多种格式的symfony日期间隔


Symfony date interval with multiple formats

我有一个名为 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生成工人的退休年份(出生日 + 60 年)。
getTimeToRetirement给出某人退休的剩余时间,formatTimeInterval($,$)用于格式化日期间隔。

现在我需要能够以不同的格式使用此 dateInterval,例如,有时我只需要将年份作为整数与视图中的其他值进行比较,因此我需要将今年作为单个值在视图上访问。

我已经尝试了一些方法,但我不知道正确的方法,所以我没有得到这个问题的更好结论。

有时我需要月份和日期来完成其他任务,而不仅仅是年份,这个值应该可以在视图上访问,因为在控制器中我只是搜索对象的 ArrayColletion 并将其传递给视图,否则我必须上去并 dow 数组集合。

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

我认为您最好让 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

这是我

所做的,但可能不是最实用的方法:

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

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