How do I show the notification when -30 days left to expire

Asked

Viewed 692 times

0

I have this condition to show the notification when the expiration date is 30 days away, but it is not doing what I want. Someone has 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 %}
  • I don’t know how Entity works, but you can get the current date date('Y-m-d'), convert the product date in the database to the format AAAA-MM-DD with PHP date_format('01-01-2016', 'Y-m-d') and calculate the difference between them.

  • Goodnight Developed, Entity.isExpired is the expiration date I set when registering a product. and the Entity.datainput is the registration date, it is taken from the database(Timestamp)..

  • So, you just need to calculate the difference between the expiration date and the current date in days, no?!

2 answers

2

If the object has an attribute with the date of entry (in this case, $dataentrada), just 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 method \DateTime::diff() returns an object of type \DateInterval whose attribute d is the difference in days).

  • he is returning me all products, but I just want him to return the products with are with less than 30 days to expire

  • From what you asked, you want to receive all products and display the notification to those that are about to expire. If you only want to search for products that are about to expire, you need to create a method in the product repository to find those that are about to expire. Improve your question.

  • Yes.eu that I want him to show me only the products that are under 30 days to expire, depending on the approaching expiration date.

  • How you search the products, today?

  • As I have an expiration date,then I tell it if if Entity.isExpired then shows the output id elseif Entity.isExpired|date('-30 days') shows the product id. that is I want it to show only those who have less -30 to the expiration date

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, means that 230 days have passed since expiration. If positive, for example, +27, means 27 days to expire.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.