php - 我想显示列状态 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>
解决方案
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
}
推荐阅读
- java - 爪哇。如何使用 forEach 检查元素是否已成功添加到集合和跟踪索引中?
- android - 在Android中旋转设备后片段字段为NULL
- javascript - 在前端选择可搜索的属性
- r - 以编程方式在R中的`::`调用中切换包
- java - JAXB ClassNotFoundException 使用 Java 11 构建 Spring Boot App 2.2.0
- pandas - groupby.rolling.count() 产生“非唯一多索引”异常
- ruby-on-rails - seed.rb 中未初始化的常量 *ModelXYZ*。错误的配置?
- java - 在 Instagram4Android 上使用 Observable
- javascript - 最小公倍数 - for 循环崩溃 - javascript
- algorithm - 根据形状的已知尺寸消除歪斜/扭曲