行条件格式PHP

时间:2019-01-06 00:15:30

标签: php html mysql datetime

我需要基于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>

数据库日期中的字段是设置为DATETIME的附加列 我不断收到错误

注意:不能在日期为bla / bal的第50行中将DateTime类的对象转换为int

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

2 个答案:

答案 0 :(得分:2)

您可以使用PHP中的Unix Time函数和MySQL中的time()函数以UNIX_TIME()格式转换所有时间信息,并以此方式工作:

<?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>

答案 1 :(得分:-1)

您需要将苹果与苹果进行比较。 如果您的MySQL日期字段的类型为date / datetime,则可以将其作为参数传递给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";