2014-02-06 36 views
0

這段代碼有什麼問題?我想突出顯示從現在開始+ 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提前任何幫助。

+0

你沒有做任何比較每個日期,你做一個比較。創建一個函數,並傳入當前日期以確定高亮類,或者您可以簡單地在jQuery中執行此操作。 –

+0

你總是比較「2014-02-01」,你想比較的日期「2014-02-10」和「2014-06-10」從未在你的代碼中進行比較。 – Curlas

+0

我無法找到我寫的錯誤...你能給我一個工作的例子嗎? – Johan

回答

0

試試這個

$date_in_two_weeks = strtotime('+2 week');

,而不是

$date_in_two_weeks = strtotime('+2 weeks');

你必須WIRTE week而不是weeks爲獲得未來兩個星期正確的時間戳

+0

nope,所有日期得到highligted – Johan

+0

將因差異大於14.嘗試將比較日期更改爲2014-02-15 –