簡體   English   中英

行條件格式化 PHP

[英]Row conditional formatting PHP

我需要基於現在時間和時差的行條件格式(背景)的解決方案,在 48 小時之前,84 例如:如果單元格中是今天的日期和時間 13:00 /背景顏色 =#ffffff 如果單元格中的值為 -48如果單元格中的值為 -84 不同顏色,則小時不同顏色。 這是我所擁有的

<?php

$con = mysqli_connect("localhost", "test", "test");
    if (!$con) {
        die("Database Connection failed".mysqli_error());
    } else {
        $db_select = mysqli_select_db($con, 'test2');
        if (!$db_select) {
            die("Database selection failed".mysqli_error());
        } else {
        }
    }

$records = mysqli_query($con, "SELECT * FROM test2 ORDER by user_Name");
$timenow = new DateTime();

?>

<!DOCTYPE html>
<html>
<body> 

<table width="500" border="2" cellpadding="2" cellspacing='1'>
    <tr bgcolor="#2ECCFA">
        <th>User</th>
        <th>Device</th>
        <th>Last Contact</th>
    </tr>



<?php

if ($records) {
    while ($row = mysqli_fetch_assoc($records)) {
        $calc = $timenow - $row['date'];
        if ($calc > 12000) {
            $rowClass = "oldOrder";
        } else {
            $rowClass = "normalOrder";
        }
        echo '<tr class="'.$rowClass.'"><td>';
        echo $row['user_name'] . '';
        echo '</td><td>';
        echo $row['Device_ID'] . '';
        echo '</td><td>';
        echo $row['Last_Contact'] . '';
        echo '</td></tr>';
    }
}

?>
    <a href=”rowClass.css” rel=”stylesheet” type=”text/css”>
</table>

DB 日期中的字段是設置為 DATETIME 的附加列我不斷收到錯誤

注意:類 DateTime 的對象無法在 bla/bal 行 50 中轉換為 int,即這部分

while ($row = mysqli_fetch_assoc($records)) {
    $calc = $timenow - $row['date'];
    if ($calc > 3) {
        $rowClass = "oldOrder";
    } else {
        $rowClass = "normalOrder";

您可以使用 PHP 中的time()函數和 MySQL 中的UNIX_TIME()函數以Unix 時間格式轉換所有時間信息,並以這種方式工作:

<?php

$con = mysqli_connect("localhost","test","test" 'test2');

if ($con->connect_error) {
    die('Connect Error: ' . $con->connect_error);
}

$records = mysqli_query($con,"SELECT *, UNIX_TIMESTAMP(date) AS u_date FROM test2 ORDER by user_Name");
$timenow = time();

?>

<!DOCTYPE html>
<html>
<body> 

<table width="500" border="2" cellpadding="2" cellspacing='1'>
    <tr bgcolor="#2ECCFA">
        <th>User</th>
        <th>Device</th>
        <th>Last Contact</th>
    </tr>

<?php

if ($records) {
    while ($row = mysqli_fetch_assoc($records)) {
        $calc = $timenow - $row['u_date'];
        if ($calc > 12000) {
            $rowClass = "oldOrder";
        } else {
            $rowClass = "normalOrder";
        }
        echo '<tr class="'.$rowClass.'"><td>';
        echo $row['user_name'] . '';
        echo '</td><td>';
        echo $row['Device_ID'] . '';
        echo '</td><td>';
        echo $row['Last_Contact'] . '';
        echo '</td></tr>';
    }
}

?>
    <a href="rowClass.css" rel="stylesheet" type="text/css">
</table>

你需要比較蘋果和蘋果。 如果你的 MySQL 日期字段是日期/日期時間類型,你可以將它作為參數傳遞給 DateTime 並比較對象:

$dateToCompare = new \DateTime("-3 days"); // 3 days ago
while ($row = mysqli_fetch_assoc($records)) {
    if ($dateToCompare > (new \DateTime($row['date']))) {
        $rowClass = "oldOrder";
    } else {
        $rowClass = "normalOrder";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM