我希望状态与day_left
有关系,例如如果day_left
有30天status
应该是danger
。这是功课
<?php
$con2= mysqli_connect("localhost","root","root","database") or die("Error: " . mysqli_error($con2));
$query2 = "SELECT *,datediff (mainexpire,now()) as day_left from contact";
$result2 = mysqli_query($con2, $query2);
echo "<table border='1' align='center' width='1000'>";
echo "<tr bgcolor='#FFFACD'><td><p><center><b>no</center></td></p></b><td><p><center><b>maintenance items
</center></td></p></b><td><p><center><b>owner
</td></p></center></b>
<td><p><center><b>detail
</center></td></p></b><td><p><center><b>expire_date
</center></td></p></b><td><p><center><b>Days Left
</center></td></p></b><td><p><center><b>Status
</center></td></p></b>
</tr>";
while($row2 = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td><center><p>" .$row2["no"] . "</center></td></p> ";
echo "<td><center><p>" .$row2["mainitem"] . "</center></td></p> ";
echo "<td><center><p>" .$row2["mainowner"] . "</center></td></p> ";
echo "<td><center><p>" .$row2["maindetail"] . "</center></td></p> ";
echo "<td><center><p>" .$row2["mainexpire"] . "</center></td></p> ";
echo "<td><center><p>" .$row2["day_left"] . "</center></td></p> ";
}
?>
</body>
嗯,你正在做好这项功课(当然,如果你自己做的那样)。只需要为if else
添加status
条件。在你的while
循环的最后一行之后有一些这样的事情。
if( $row2["day_left"] > 30 ) {
echo "<td><center><p>All OK</center></td></p> ";
} else {
echo "<td><center><p>Danger</center></td></p> ";
}
function day_left($day){ //$day will be the target date
$target_date= strtotime($day); //this convert the target day to second
$today = strtotime("today"); //this convert the current day to second
$day_sec = $target_date- $today; //so target day minus current day second
$total_day_left = $day_sec / 86400; //divide it to 86400 thats equivalent to one day
return $total_day_left; //the total day left
}