繁体   English   中英

php中带有时间戳比较的条件格式html表

[英]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 分钟。 任何帮助将非常感激。 我什至不知道从哪里开始。

谢谢

编辑:我的时间戳格式“2015-07-17 19:17:31”

执行 if 以查看时间是否超过 60 分钟,如果是,则为其分配一个具有不同背景颜色的类。 由于您没有澄清,我将假设您使用的是 unix timestamp 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,您会在几分钟内得到它。

$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 Header 中定义这两种样式,例如:

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

就是这样。 希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM