【问题标题】:Change a td date color with PHP使用 PHP 更改 td 日期颜色
【发布时间】:2022-07-21 19:19:15
【问题描述】:

我必须根据当前日期更改颜色 td 的 date_client。 例如,如果今天是生日的客户,则此 td s'date 变为红色(字体,而不是背景)。

我的 HTML

<?php foreach ($rows as $row) : ?>
      <tr id_client=<?php echo $row["id_client"]; ?>>
        <td><?php echo $row["id_client"]; ?></td>
        
        <td><?php echo $row["client_birth_date"]; ?></td>
      </tr>
    <?php endforeach; ?>

我的数组 https://i.stack.imgur.com/Hpzwl.png

由于我处于循环 foreach 中,因此每个生日都是唯一的。

我已经在 PHP 中尝试过。 PHP

 function date_color(){
      global $conn;
      $id_client = $_POST["id_client"];
      $client_date = $_POST["client_birth_date"];
      $calendar=$_POST["calendrier"];
    
      $calendar->date_format('Y-m-d'). $client_date->date_format('Y-m-d');
    
      if ($calendar==$client_date){
        echo "<font color='#FF0000'>$client_date</font>";
      }
        
    }

日历是我屏幕上输入的日期。

我也不知道如何在 JS 中做。 (障碍是如何准确获取 td 的出生日期)

有什么想法吗?

谢谢。

【问题讨论】:

    标签: php css arrays


    【解决方案1】:

    您可以将数据库日期与包含今天日期的变量进行比较。如果它们匹配,则将类应用于&lt;td&gt;。注意:数据库日期和今天的日期必须采用相同的格式 (Y-m-d)。

      //create todays date once
    $today = date('Y-m-d');
    
    foreach ($rows as $row){
      $id=$row["id_client"];
      $birthday = $row["client_birth_date"];
      
        //create empty class
      $class = '';
        //compare birthday with today. If equal, set a class class
      if( $birthday == $today ){
        $class =' class="font_red"';
        }
      
        //output
      echo '<tr id=".$id.'">
              <td>'.$id.'</td>
              <td'.$class.'>'.$birthday.'</td>  //<-- apply class
            </tr>';
        }
    

    还有css

    .font_red{
      color: red;
      }
    

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      相关资源
      最近更新 更多