Seyker Seyker - 1 year ago 39
HTML Question

Color based on statement

I am making a form in which it saves the creation date.
And I want to make a color change depending on the date.
Example:
1 day ago: green
2 days ago: yellow
3 days ago: red

<?php
while ($registro = mysql_fetch_object($consulta)) {
echo "<tr>";
echo "<td>$registro->consulid</td>";
echo "<td>$registro->consultatipo</td>";
echo "<td>$registro->fechacreada</td>";
echo "<td><div id='color'></div></td>";
echo "<td><a href='consultar.php?idconsulta=$registro->consulid&fecha=$registro->fechacreada&tipoc=$registro->consultatipo'> <img src='IMG/edit.png' height='20' border='0px' width='20'> </a></td>";
echo "</tr>";
}
mysql_close($conexion);
?>


I've this code and i want to add the color on the div color and the paramater date is $registro->fechacreada
My question is how i do it on JavaSript?

Answer Source

Add a field called inTime and set it with the value stored in JavaScript variable time by executing this code var time = new Date().getTime(); function in JavaScript at the time of insertion of data. Then when fetching data, Check that

var a = new Date().getTime();
if(a >= ($time)+86400000 && a< ($time)+172800000){
  // 1 day old
  // color code here
}
else
if(a >= ($time)+172800000){
  // older than 1 day
}

Note that ($time) should be replaced with value of inTime field in table

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download