基于有效性的PHP表(日期)动态颜色更改

时间:2018-10-10 05:59:47

标签: php mysql

<div class="table-responsive">  
<table id="Well_CAT" class="table table-striped table-bordered">                              
<thead> <th>Client_Contract_Number</th>
<th>Currently_Using</th>
<th>MBPS_EAM_Number_RIGT</th>
<th>Model_and_Type</th>
<th>LFour_Yearly</th>
<th>Six_Monthly</th>
<th>One_Yearly</th>
<th>One_and_Half_Yearly</th>
<th>Two_Yearly</th>
<th>Two_and_Half_Yearly</th>
<th>Three_Yearly</th>
<th>Three_and_Half_Yearly</th>
<th>Four_Yearly</th>
<th>Remarks</th>
</thead>  
<?php
while($rows=mysql_fetch_array($result)){
?><tr>  
 <td class="exdate"><? echo $rows['Client_Contract_Number']; ?></td>
 <td class="exdate"><? echo $rows['Currently_Using']; ?></td>
 <td><? echo $rows['MBPS_EAM_Number_RIGT']; ?></td>
 <td><? echo $rows['Model_and_Type']; ?></td>
 <td><? echo $rows['LFour_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Six_Monthly']; ?></td>
 <td class="exdate"><? echo $rows['One_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['One_and_Half_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Two_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Two_and_Half_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Three_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Three_and_Half_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Four_Yearly']; ?></td>
 <td class="exdate"><? echo $rows['Remarks']; ?></td>
  </tr>  
    <?php
     }
     ?>
  </table>

下面是我的表格,它用于跟踪证书的有效性,从Lfour每年到Fouryearly的列都是日期字段,我想根据有效性对这些字段进行有条件的颜色设置...像如果有效-绿色,已过期-红色。

1 个答案:

答案 0 :(得分:1)

由于PHP> = 5.2.0,您可以这样使用DateTime类:

if (new DateTime() > new DateTime("2010-05-15 16:00:00")) {
    # current time is greater than 2010-05-15 16:00:00
    # in other words, 2010-05-15 16:00:00 has passed
}

根据这些规则解析传递给DateTime构造函数的字符串。

以下是您的代码示例:

<td class="exdate" style="color:<?php echo (new DateTime() > new DateTime($rows['Six_Monthly'])) ? 'red' : 'green'; ?>;" ><? echo $rows['Six_Monthly']; ?></td>