This question already has an answer here:
Hi i have searched alot in my quest to find a script that colors the dates from today untill +2 weeks. All the dates between today (for example 2014-02-06 and 2014-02-20) should get marked color red. Also dates in the past must be marked orange. all other dates stay unclored. I cant find a script to get it to work and i am out of ideas with my knowledge
<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>
i have tried something like this, but this doesnt work....
// 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 = "";
}
</div>
Try something like this:
// 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 = "";
}
Then later in your code you use $highlight_css_class like this:
<td class="<?php echo $highlight_css_class; ?>">...</td>
I do not have points to comment, so I have to write an answer. Plz note I do not have the right answer, but I may have some ideas that could help you in the right direction.
First of. Try to give all your TR's ID's (if possible) AND CLASS names!
Now get the current date by:
var d = getDate();
This will give you the day and time of the day in ms since jan 1st 1970. Now there goes 1 209 600 000 ms on 2 weeks (store that as var 2weeks), so now you have something to compare!
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";
}
}
Now the classes SHOULD have been set for the different events, now do the rest in 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
}
However this require you to convert the dates you want to compare to ms since jan 1st 1970 in order to have something to compare with. The great thing about ms since jan 1st 1970 is that it is easy to compare dates even with leap years and across months (should one date be from jan 30th and the other today for instance).
This was just my short brainstorm. I would be amazed if you can take it to good use right away, but hopefully it can send you off in a direction that might actually work.
Others, feel free to comment and/or come with corrections to my input.
Here is a fully working sample.
Take a look at this fiddle: 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>