這是代碼 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> 如果到期日期是當月,則狀態應更改為橙色背景顏色。怎么辦,請幫忙!
1 回答

德瑪西亞99
TA貢獻1770條經驗 獲得超3個贊
首先,比較月份,如果相同,則將 'orangeClassName' 添加到 php 變量:
$tdStyle = ($currentMonth == date('M',strtotime(the_field('expire_date')))) ? 'orangeClassName' : '';
然后將該變量包含在您的:
<td class="<?php echo $tdStyle;?>"><?php the_field('expire_date')?></td>
并將相關類添加到您的CSS中:
.orangeClassName {background:orange;}
請注意,這只是檢查月份是否相同,因此 04-2020 == 04-2019 也會返回 true
- 1 回答
- 0 關注
- 113 瀏覽
添加回答
舉報
0/150
提交
取消