<分区>
标签 javascript php jquery css
<分区>
您好,我已经搜索了很多,以找到一个脚本,该脚本可以为从今天到 +2 周的日期着色。今天之间的所有日期(例如 2014-02-06 和 2014-02-20)都应该标记为红色。过去的日期也必须标记为橙色。所有其他日期保持不变。我找不到一个脚本来让它工作,而且据我所知我没有想法
<TABLE>
<TR><TD>item 1</TD><TD>2014-02-12</TD></TR>
<TR><TD>item 2</TD><TD>2014-06-17</TD></TR>
<TR><TD>item 3</TD><TD>2014-01-12</TD></TR>
<TR><TD>item 4</TD><TD>2015-08-12</TD></TR>
</TABLE>
我已经尝试过类似的方法,但这不起作用....
// 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 = "";
}
最佳答案
尝试这样的事情:
// get the date to compare, from db or whatever you want
$date_to_compare = "2014/02/02";
//today
$dateNow = new DateTime("now");
//date to compare
$dateCompare = new DateTime($date_to_compare);
//lets find difference between today and date to compare:
$difference = $dateNow->diff($dateCompare);
//for debugging; %R gives you prefix (- or +); %a gives you days.
//source: php.net/manual/en/datetime.diff.php
echo "Days in difference (from today until compare date): " . $difference->format('%R%a') . "<br />";
if ($difference->format('%R%a') < 0) {
$highlight_css_class = "orange"; //If difference is less than 0.
} elseif ($difference->format('%R%a') <= 14) {
$highlight_css_class = "red" //"Mark this date Red, date is in less than 2 weeks (14 days)";
} else {
$highlight_css_class = "";
}
然后在你的代码中你像这样使用 $highlight_css_class:
<td class="<?php echo $highlight_css_class; ?>">...</td>
关于javascript - 如何在到期日期突出显示行的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21602284/