首页 > 解决方案 > 我想显示列状态 ex.if 剩下的天有 30 天状态它很危险

问题描述

我希望 status 有关系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>

标签: phpmysqlhtml

解决方案


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
}

推荐阅读