Fechas de vencimiento con php mysql
Quiero hacer una alerta de vencimiento mostrada en un campo de una tabla llamado vencimiento por el momento e logrado que me aparezca cuando faltan 2 días por vencer pero no logro hacer cuando falte un día y cuando aun faltan varios días me pone que faltan 2 y eso esta mal como logro hacer que mis alertas funcionen correctamente.
Código de tabla :
<div class="dataWrapper">
<table class="table table-hover table-sm table-bordered table-condensed" id="exportar">
<div class="form-row">
<div class="form-group col-md-3"></div>
<div class="form-group col-md-3">
<label for="">Fecha Inicio del Mtto</label>
<input id="min" name="min" type="text" placeholder="Fecha iniciar busqueda" class="form-control">
</div>
<div class="form-group col-md-3">
<label for="">Fecha Inicio del Mtto(cerrar rango)</label>
<input id="max" name="max" type="text" placeholder="Fecha cerrar busqueda" class="form-control">
</div>
</div>
<thead class="thead-dark text-center">
<tr>
<th>ID_mtto</th>
<th>Vehículo</th>
<th>Servicio</th>
<th>Tipo_Mtto</th>
<th>Observaciones</th>
<th>Fecha Inicio</th>
<th>Fecha Final</th>
<th>Vencimiento</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","root","","controldeflotilla") or die (mysqli_error());
$query="SELECT * FROM mantenimiento ORDER BY id_mtto DESC";
$result=mysqli_query($con, $query) or die (mysqli_error());
$fecha_actual = new DateTime(date('Y-m-d'));//nueva variable para vencimiento//
while ($row=mysqli_fetch_array($result)){
$fecha_final = new DateTime($row['fecha_final']);
$dias = $fecha_actual->diff($fecha_final)->format('%r%a');
// Si la fecha final es igual a la fecha actual o anterior
if ($dias <= 0) {
$status= "Mantenimiento vencido";
}
elseif ($dias <= 2) {
$status= "Está a " . $dias . " días de vencer";
}
elseif ($dias <=1) {
$status= "mañana vence";
}
$datos=$row['id_mtto']."||".
$row['vehiculo']."||".
$row['servicio']."||".
$row['tipo_mtto']."||".
$row['observaciones']."||".
$row['fecha_inicio']."||".
$row['fecha_final'];
?>
<tr>
<td class="text-center"><?php echo$row['id_mtto']?></td>
<td class="text-center"><?php echo$row['vehiculo']?></td>
<td class="text-center"><?php echo$row['servicio']?></td>
<td class="text-center"><?php echo$row['tipo_mtto']?></td>
<td class="text-center"><?php echo$row['observaciones']?></td>
<td class="text-center"><?php echo$row['fecha_inicio']?></td>
<td class="text-center"><?php echo$row['fecha_final']?></td>
<td class="text-center">
<?php echo $status?>
</td>
</tr>
<?php
}
mysqli_query($con,$query) or die ("Problemas al llamar tabla".mysqli_error());
mysqli_close($con);
?>
</tbody>
</table>
</div>
1 Respuesta
Respuesta de Carlos M-G
