条件格式HTML表在PHP与时间戳比较


conditional formatting html table in php with time stamp comparison

echo '<table style="width:100%"> <tr>';
echo '<td>Order</td>'; 
echo '<td>Destination</td>';
echo '<td>Location</td>';
echo '<td>Status</td>';
echo '<td>TimeStamp</td>';
echo '</tr>';
if($result) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>';
    echo $row['OrderNumber'] . '';
    echo '</td><td>';
    echo $row['Destination'] . '';
    echo '</td><td>';
    echo $row['Location'] . '';
    echo '</td><td>';
    echo $row['Status'] . '';
    echo '</td><td>';
    echo $row['TimeStamp'] . '';
    echo '</td></tr>';
}
echo '</table>';

}

我想改变背景的行变成不同的颜色是时间戳超过60分钟过去的当前时间。任何帮助都将非常感激。我甚至不知道从哪里开始。

感谢

edit: format of my time stamp "2015-07-17 19:17:31"

查看时间是否超过60分钟,如果超过60分钟,则为其分配一个不同背景颜色的类。由于您没有说明,我将假设您使用的是unix时间戳time()

$currTime = time();
if($result) {
while($row = mysqli_fetch_assoc($result)) { 
    $calc = $currTime - $row['TimeStamp'];
    if($calc > 3600){
    $rowClass = "oldOrder";
    } else {
    $rowClass = "normalOrder";
    }
echo '<tr class="'.$rowClass.'"><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}

然后添加CSS定义两个类

.oldOrder{
background-color: #ccc;
}
.normalOrder{
background-color: #fff;
}
$NowDT = date("Y-m-d H:i:s");
$NowRDT = strtotime($NowDT);
$DateTimeNF = $row['TimeStamp'];
$TimeRF = strtotime($DateTimeNF);
$TimeDifference = $NowRDT - $TimeRF;
$TimeDifferenceHours = $TimeDifference/3600;
If ($TimeDifferenceHours > "1") 
{
echo '<tr bgcolor="#E60000"><td>';
}
else
{
echo '<tr><td>';
}

您的时间戳似乎是一个字符串,您需要使用strtotime将字符串转换为Unix时间戳:

$rowTime=strtotime($row['TimeStamp']);

,然后在实际时间中减去它,作为之前的答案,得到以秒为单位的差值。把它除以60,就得到1分钟。

$currenTime=time();
$diffSeconds=$currenTime-$rowTime;
$diffMinutes=$diffSeconds/60;

,然后检查差值是否大于60:

$myCSS="normalRow";
if ($diffMinutes>60) { $myCSS="differentRow"; }

,然后使用CSS样式($myCSS)在您的表行:

echo '<tr class="'.$myCSS.'"><td>';

你需要在你的CSS文件或HTML头文件中定义这两种样式,如:

<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>

就是这样。