繁体   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