How do I show the notification when it is missing -30 days to the expiration date

0

I have this condition to show the notification when the expiration date is missing 30 days, but it is not doing what I want. Anyone have a better idea?

{% if entity.isExpired %}

          strong>Alerta!</strong>O produto com o código {{ entity.id }} esta com a data de validade vencida 

{% elseif entity.dataentrada == date(entity.isExpired)|date("-30 days") %}

          <strong>Alerta!</strong>  <a href="{{ path('armazem_showatual', { 'id': entity.id }) }}"> O produto com o código {{ entity.id }} esta com a pouco dias para fechar

                   {% endif %}
    
asked by anonymous 08.05.2016 / 22:53

2 answers

2

If the object has an attribute with the date of entry (in this case, $dataentrada ), simply use it to calculate whether the object is expired or not:

/**
 * @return bool Se o objeto está expirado.
 */
public function isExpired()
{
    return $this->dataentrada->diff(new \DateTime())->d > 30;
}

(remembering that the \DateTime::diff() method returns an object of type \DateInterval whose d attribute is the difference in days).

    
09.05.2016 / 13:04
2

Sample code:

<?php

$data_atual = new DateTime(date('Y-m-d'));
$data_expiracao = new DateTime('2016-10-01');

$intervalo_em_dias = $data_atual->diff($data_expiracao);

echo $intervalo_em_dias->format('%R%a dias');

If the result is negative, for example, -230 , that means that it has already passed 230 days after the expiration. If it is positive, for example, +27 , that means 27 days to expire.

    
08.05.2016 / 23:17