conditional formatting html table in php with time stamp comparison

后端 未结 3 1572
梦如初夏
梦如初夏 2021-01-28 03:03
echo \'\';
echo \'\'; 
echo \'\';
echo \'
相关标签:
3条回答
  • 2021-01-28 03:40

    Do an if to see if time is over 60 minutes and if so assign it a class with a different background color. Since you didn't clarify I'm going to assume you are using 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>';
    }
    

    Then add CSS to define the two classes

    .oldOrder{
    background-color: #ccc;
    }
    .normalOrder{
    background-color: #fff;
    }
    
    0 讨论(0)
  • 2021-01-28 03:40
    $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>';
    }
    
    0 讨论(0)
  • 2021-01-28 03:41

    It seems like your timestamp is a String, you need to convert your string to a Unix TimeStamp using strtotime:

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

    and then substract it from the actual time, as the previous answer, to obtain the difference in seconnds. Divide it by 60 and you get it on minutes.

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

    and then check if the difference is bigger than 60:

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

    and then use that CSS Style ($myCSS) in your table row:

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

    You need to define those two styles in your CSS file or your HTML Header like:

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

    And that's it. Hope it helps.

    0 讨论(0)
提交回复
热议问题
OrderDestinationLocation