1) To calculate the date by php :
Locate where the while you use to show the results and right after you place:
<?php
$fecha_hoy= date("Y-m-d"); // fecha de hoy va antes del while para que la obtenga sólo una vez
function calcular_dias($fecha_hoy, $fecha_hasta) // esta función la puedes colocar en un archivo aparte, imagino tendras un archivo para todas tus funciones
{
$diferencia_dias = (strtotime($fecha_hoy)-strtotime($fecha_hasta))/86400; // transformación para poder restar las fechas sin tener problemas con años bisiestos y demás
$diferencia_dias = abs($diferencia_dias); // Para garantizar que el número de dias de positivo (valor absoluto)
$diferencia_dias = floor($diferencia_dias); // Redondeo hacia abajo para garantizar no contar 1 día de más
return $diferencia_dias; // retornamos el total de días que necesitabas
}
while ($fila=mysql_fetch_array($rst_informe)){ //despues de esta línea de tu código
$fecha_hasta=$fila['SOAT_HASTA']; // fecha hasta proveniente de la bd
$diferencia_dias= calcular_dias($fecha_hoy, $fecha_hasta); //implementamos la función
?>
2) You place the calculation result of days in your input of name="VENCE"
like this:
<input type="text" value="<?php echo $diferencia_dias; ?>" name="VENCE" id="VENCE"/>
3) Then you place the javascript validation of the colors you have (green or red) so you can scroll through and paint all the inputs and not just one as it is happening to you right now.
I had to do it this way because you have another error, the value of the attribute "id" of the elements you need is the same for everyone ( id="VENCE"
). Here is the solution
function calculo(){
var inputs = document.getElementsByTagName("input");
for (i=0; i < inputs.length; i++){
if (inputs[i].value > 335 && inputs[i].name=='VENCE' ){
inputs[i].style.backgroundColor = "red";
inputs[i].style.color = "white";
}
if (inputs[i].value <= 335 && inputs[i].name=='VENCE' ){
inputs[i].style.backgroundColor = "green";
inputs[i].style.color = "white";
}
}
}
OBSERVATION: Although the step that I will explain is not necessary, it would be good to execute it so that there is no bad implementation of the "id" field in your forms
Next time, please consider placing a "unique" value such as "id" to each HTML element you have on your form. A simple way to achieve this in your code would be to create a "counter" that allows you to convert your id="VENCE"
to VENCE1, VENCE2, VENCE3, etc. concatenating the word wins with the counter we created. This counter will automatically increase because we use it within the while.
To create that variable that offers dynamism to "id" we use:
$unico= 'VENCE'.$i; // $i es un incremental que empieza en cero y aumenta de
//1 en 1
and to be able to place this value within the "id" of each input we use:
<input type="text" value="<?php echo $diferencia_dias; ?>" name="VENCE" id="<?php echo $unico; ?>
like this
<body onload="calculo()">
<?php
function calcular_dias($fecha_hoy, $fecha_hasta) // esta función la puedes colocar en un archivo aparte, imagino tendras un archivo para todas tus funciones
{
$diferencia_dias = (strtotime($fecha_hoy)-strtotime($fecha_hasta))/86400; // transformación para poder restar las fechas sin tener problemas con años bisiestos y demás
$diferencia_dias = abs($diferencia_dias); // Para garantizar que el número de dias de positivo (valor absoluto)
$diferencia_dias = floor($diferencia_dias); // Redondeo hacia abajo para garantizar no contar 1 día de más
return $diferencia_dias; // retornamos el total de días que necesitabas
}
$i=0; // contador
while ($fila=mysql_fetch_array($rst_informe)){
$fecha_hasta=$fila['SOAT_HASTA']; // fecha hasta proveniente de la bd
$diferencia_dias= calcular_dias($fecha_hoy, $fecha_hasta); //implementamos la función
$unico= 'VENCE'.$i;
$i++;
?>
<td><input value="<?php echo $fila['PLACA']; ?>" type="text" name="PLACA" id="PLACA"></td>
<td>
<select name="GRUPO" id="GRUPO">
<option value="<?php echo $fila['GRUPO']; ?>"><?php echo $fila['GRUPO']; ?></option>
</td>
<td><input type="text" value="<?php echo $fila['TIPO_VEHICULO']; ?>" name="TIPO_VEHICULO" id="TIPO_VEHICULO"></td>
<td><input type="text" value="<?php echo $fila['SOAT_DESDE']; ?>" name="SOAT_DESDE" id="SOAT_DESDE"/>-
<input type="text" value="<?php echo $fila['SOAT_HASTA']; ?>" name="SOAT_HASTA" id="SOAT_HASTA"/></td>
<td><input type="text" value="<?php echo $diferencia_dias; ?>" name="VENCE" id="<?php echo $unico; ?>"/></td>
<?php
}
?>
Here is the example of inputs with javascript only for you to observe the solution:
var inputs = document.getElementsByTagName("input");
for (i=0; i < inputs.length; i++){
if (inputs[i].value > 335 && inputs[i].name=='VENCE' ){
inputs[i].style.backgroundColor = "red";
inputs[i].style.color = "white";
}
if (inputs[i].value <= 335 && inputs[i].name=='VENCE' ){
inputs[i].style.backgroundColor = "green";
inputs[i].style.color = "white";
}
}
<!--inputs con nombre "VENCE" -->
<input type="text" value="200" name="VENCE" id="VENCE1">
<input type="text" value="455" name="VENCE" id="VENCE2">
<input type="text" value="677" name="VENCE" id="VENCE3">
<input type="text" value="212" name="VENCE" id="VENCE4">
<input type="text" value="355" name="VENCE" id="VENCE5">
<!--inputs sin nombre "VENCE" -->
<input type="text" value="355" name="otro" id="otro">
<input type="text" value="355" name="prueba" id="prueba">
<input type="text" value="355" name="novence" id="novence">
That's it. A greeting!