I have a code where I compare the current date with the start date of a request and depending on the days until the start of the request, I have to show an image to alert the user (green, yellow or red alert). The problem I have is that for some reason when the start date has passed it should not show any alert but in my code if the date that has passed is less than 30 days it shows the green alert. And the first IF validates that to enter the start date it must be greater than or equal to the current date. My code is the following:
$fecha_inicio = date('d-m-Y');
if($fecha_inicio >= $fechaActual)
{
//retorna la cantidad de segundos entre las fechas
$diff = abs(strtotime($fecha_inicio) - strtotime($fechaActual));
// para pasarlo a dias 60 segundos tiene un minuto y una hora 60
// minutos y un dia 24 horas
$total = $diff/(60*60*24);
if($total <= 14 && $total > 7)
{
<img class="alerta" src="img/alerta_amarilla.png">
}
else if($total <= 7)
{
<img class="alerta" src="img/alerta_roja.png">
}
else if($total >= 15 && $total < 30)
{
<img class="alerta" src="img/alerta_verde.png">
}
}
You are making a very common mistake: using localized date formatting and making text comparisons between them.
You should get used to using dates in ISO 8601 format or equivalent (such as
AAAA-MM-DD HH:MM:SS
), although ISO is always recommended and, if possible, in UTC.In this way, the comparison of character strings would be equivalent to the comparison of dates, since the magnitudes of time would be compared from greater to lesser (year, month, day, hour, minutes and seconds).
Your code would be: