用php if else语句改变td的颜色

Change of td color with php if else statement

本文关键字:td 颜色 改变 语句 php if else      更新时间:2023-09-26

我想根据从sql查询收到的日期更改td的颜色。请帮助以下-

如果获取的日期小于今天的日期,我想将td的颜色更改为绿色。

<?php
    if($mydate > date('d-M-Y'))
    {
        echo "<td style="background-color:green;">";
    }
    else
    {
       echo "<td>";
    }
?>

不能以字符串格式检查日期。

可以这样做的一种方法是通过在UNIX时间戳中格式化日期(这样您就有一个整数),允许您正确检查一个日期是否实际上比另一个日期更新或更早。

$mydate = (new DateTime($mydate))->format('U');
$date   = (new DateTime(date('Y-m-d')))->format('U');
if($mydate > $date)
{
    echo "<td style='background-color: green;'>";
}
else
{
    echo "<td>";
}

首先,将两个时间转换为单一格式并比较

尝试后

为了方便使用,我使用div而不是td

<?php
    $mydate="08-sep-2015";
    //$mydate="11-sep-2015";
    $today     = date('d-M-Y');
    $todayTime = strtotime($today);
    $compTime  = strtotime($mydate);
    if($compTime < $todayTime) {
      echo "<div style='background-color:green;'>fgd</div>";
    }
    else{
      echo "<div>sdfsdf</div>";
    }
?>
$mydate = "2015-10-01";
if(strtotime($mydate) > strtotime(date('Y-m-d')))
{
    echo "<td style='background-color:green;'>testing</td>";
}
else
{
   echo "<td>testing</td>";
}

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <table>
    <tr>
      <?php
        $mydate = "2015-10-01";
        if(strtotime($mydate) > strtotime(date('Y-m-d')))
        {
            echo "<td  bgcolor='#FF0000'>testing</td>";
        }
        else
        {
           echo "<td>NOt Green</td>";
        }
        ?>
    </tr>
  </table>
</body>
</html>