您好我已经搜索了很多寻找一个脚本,从今天开始着色日期+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 = "";
}
答案 0 :(得分:0)
尝试这样的事情:
// 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>
答案 1 :(得分:0)
我没有评论要点,所以我必须写一个答案。 Plz注意我没有正确的答案,但我可能有一些想法可以帮助你朝着正确的方向发展。
第一个。尝试提供所有TR的ID(如果可能)和CLASS名称!
现在通过以下方式获取当前日期:
var d = getDate();
这将为你提供自1970年1月1日以来ms的一天中的日期和时间。现在2周内有1 209 600 000 ms(存储为var 2周),所以现在你有比较的东西!
if ( "time in ms to compare" < d) // Action to be taken for events in the past
{
var class = document.getElementsByClassName("className");
for (var i=0; i<class.length; i++)
{
class[i].className = "before_today";
}
}
else if ("time in ms to compare" >= d && "time in ms to compare" <= d + 2weeks) // Action to be taken for events within the next 2 weeks.
{
var class = document.getElementsByClassName("className");
for (var i=0; i<class.length; i++)
{
class[i].className = "within_2weeks";
}
}
else // Action to be taken (or not to be taken) for events more than 2 weeks away
{
var class = document.getElementsByClassName("className");
for (var i=0; i<class.length; i++)
{
class[i].className = "above_2weeks";
}
}
现在应该为不同的事件设置类,现在在CSS中完成剩下的工作。
.before_today
{
background-color: rgba(255, 204, 0, 0,4) //Orange with a 40% opacity
}
.within_2weeks
{
background-color: rgba(170, 0, 0, 0,4) // Dark red with a 40% opacity
}
.above_2weeks
{
background-color: rgba(0, 170, 0, 0,4) // Dark green with a 40% opacity
}
但是,这要求您将自己想要比较的日期转换为自1970年1月1日以来的ms,以便与之进行比较。自1970年1月1日以来ms的好处在于,即使是闰年和月份也很容易比较日期(例如,如果一个日期是从1月30日开始,而今天是另一个日期)。
这只是我的短暂头脑风暴。如果你能立刻好好利用它,我会感到惊讶,但希望它可以让你朝着可能真正起作用的方向发送。
其他人,请随意评论和/或对我的输入进行更正。
答案 2 :(得分:0)
这是一个完整的样本。
看看这个小提琴:http://phpfiddle.org/main/code/2xu-h6d
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample</title>
<style type="text/css">
.red {
color : #f00;
}
.orange {
color : #ffa500;
}
</style>
</head>
<body>
<table>
<?php
$dates = array('2014-01-23', '2014-02-12', '2014-06-17', '2014-01-12', '2015-08-12');
$dateNow = new DateTime("now");
foreach ($dates as $date):
// Compare the date in your list to now
$dateFromList = new DateTime($date);
$daysDifference = $dateNow->diff($dateFromList);
$class = '';
if (((int) $daysDifference->format('%r%a')) < 0):
$class = 'orange';
elseif (((int) $daysDifference->format('%r%a')) <= 14):
$class = 'red';
endif;
echo '<tr class="', $class, '"><td>', $date, '</td></tr>';
endforeach;
?>
</table>
</body>
</html>