這段代碼有什麼問題?我想突出顯示從現在開始+ 2周的所有日子。但是,他們都會用.highlight類來突出顯示。錯誤在哪裏?錯誤突出表日期
<?php
// get two weeks from now
$date_in_two_weeks = strtotime('+2 weeks');
$date_in_two_weeks = date("Y-m-d",$date_in_two_weeks);
// get the date to compare, from db or whatever you want
$date_to_compare = "2014-02-01";
// compare the date in your list to now + 2 weeks and then put the date difference into $days_difference
$date_from_list = new DateTime($date_to_compare);
$date_in_two_weeks = new DateTime($date_in_two_weeks);
$days_difference = $date_from_list->diff($date_in_two_weeks);
if ($days_difference->days > 14) {
$highlight_css_class = "highlight";
} else {
$highlight_css_class = "none";
}
?>
<style>
.highlight {
color:#cc0000;
}
.none {
}
</style>
<fieldset>
<table class="tablesorter" id="my-table" border="1" style="border-collapse:collapse">
<thead>
<tr>
<th>REK</th>
<th>BESCHRIJVING</th>
<th>VERVALDATUM</th>
</tr>
</thead>
<tbody>
<tr><td>A1</td><td>hamburger</td><td class="<?php echo $highlight_css_class;?>">2014-02-10</td></tr>
<tr><td>A1</td><td>tomato</td><td class="<?php echo $highlight_css_class;?>">2014-06-10</td></tr>
</tbody>
</table>
我測試的代碼,並改變了很多,但我不能得到它的工作.... THX提前任何幫助。
你沒有做任何比較每個日期,你做一個比較。創建一個函數,並傳入當前日期以確定高亮類,或者您可以簡單地在jQuery中執行此操作。 –
你總是比較「2014-02-01」,你想比較的日期「2014-02-10」和「2014-06-10」從未在你的代碼中進行比較。 – Curlas
我無法找到我寫的錯誤...你能給我一個工作的例子嗎? – Johan