0

I have a Date interval of an entity called worker with a period of time see the next functions:

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

The only value that is stored in the database here is $this->getBirthDay(), then getRetireYear generates the year of retirement of a worker (birthDay + 60 years).
getTimeToRetirement gives the time left to retire someone and formatTimeInterval($,$) is used to format the date Interval.

Now I need to be able to use this dateInterval in different formats, for instance sometimes I just need the year to compare as integer with other values in the views, so I need this year as a single value to be accessible on the views.

I have tried a few things but I do not know the correct way to do it, so I have not get the nicer conclusion to this issue.

Also sometimes I need the month and the day for other tasks, not only the year, this values should be accessible on the views because in the controller I just search for an ArrayColletion of objects and pass it to the view, otherwise I have to go up and dow the array collection.

For more information about the problem you can read this thread

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

2 Answers2

1

I think you would be better off having the getTimeToRetirement method return the DateInterval and move the formatTimeInterval method out of the entity altogether as it is more relevant to DateInterval formatting than to your worker. You could then have your DateInterval formatted as you want when you need it. Something along those lines.

In your worker entity class:

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

You can then use the date Twig filter to format the DateInterval in the view. See http://twig.sensiolabs.org/doc/filters/date.html If you need something more specific or encapsulated, you could also create a Twig extension for that. See http://symfony.com/doc/master/cookbook/templating/twig_extension.html

Renaud Martinet
  • 198
  • 1
  • 7
  • Ok, I get your point, but how do i make this different formats available on the views? That is my main issue. If I call this on the Controller I have to go up and down the Array Collection and that is not very efficient I think, any way maybe I'm wrong. – Abel Oct 17 '13 at 06:26
  • 1
    Of course it's better if you can format it in the view. Updated the answer to reflect that. – Renaud Martinet Oct 17 '13 at 15:26
1

this is what I have done, but may be is not the most practical way:

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

I will convert this two functions in 3 functions simpler and more reusable, and maybe create the helper if I need it from outside this environment.In the Controller I access the config file:

         $redTime = $this->container->getParameter('time_red');
         $orangeTime = $this->container->getParameter('time_orange');

Of course no I have to pass the list of workers to the view and also the config vars. And now in the view I can just access the variables as follows:

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

It works fine... with independence of the philosophy violations!?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Abel
  • 538
  • 5
  • 8
  • 28