php 中的条件格式 html table 与时间戳比较
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 分钟,如果是,则为其指定一个具有不同背景颜色的 class。由于您没有澄清,我假设您使用的是 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定义两个classes
.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"; }
然后在 table 行中使用 CSS 样式 ($myCSS):
echo '<tr class="'.$myCSS.'"><td>';
您需要在 CSS 文件或 HTML Header 中定义这两种样式,例如:
<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>
就是这样。希望对你有帮助。
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 分钟,如果是,则为其指定一个具有不同背景颜色的 class。由于您没有澄清,我假设您使用的是 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定义两个classes
.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"; }
然后在 table 行中使用 CSS 样式 ($myCSS):
echo '<tr class="'.$myCSS.'"><td>';
您需要在 CSS 文件或 HTML Header 中定义这两种样式,例如:
<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>
就是这样。希望对你有帮助。