简体   繁体   English

如何保持背景颜色的状态?

[英]How to keep background-color in the status?

This is the code这是代码

  edate = get_field('expire_date');
      $currentDateTime = date('Y-m-d');
      $currentMonth = date('M');
      $earlier = new DateTime($edate);
      $later = new DateTime($currentDateTime);

     $daysrem = $later->diff($earlier)->format("%a");
     
     $status = get_field('status');
    ?>
            <tr>
            <td class=""><?php the_field('expire_date')?></td>
            <td><?php echo $daysrem; ?></td>
            <td class="<?php the_field('status') ?>"><?php the_field('status') ?></td>
        </tr>
 

If the expire date is the current month the status should change to a background color of orange.如果到期日期是当前月份,则状态应更改为橙色背景色。 How to do it, Please Help!怎么办,求大神帮忙!

First, compare the months and if the same, add 'orangeClassName' to a php variable:首先,比较月份,如果相同,将“orangeClassName”添加到 php 变量中:

$tdStyle = ($currentMonth == date('M',strtotime(the_field('expire_date')))) ? 'orangeClassName' : '';

then include that variable in your:然后将该变量包含在您的:

<td class="<?php echo $tdStyle;?>"><?php the_field('expire_date')?></td>

and add the relevant class to your css:并将相关的 class 添加到您的 css 中:

.orangeClassName {background:orange;}

Note that this is only checking if the months are the same, so 04-2020 == 04-2019 would also return true请注意,这只是检查月份是否相同,因此 04-2020 == 04-2019 也会返回 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM