Color coding based on dates

Hello,

I would like set x as follows:

x=red when it is past due
x=yellow when there is 7 days or less to due date
x=nothing when there is more than 7 days to due date

Basically, I am comparing due date with now

Thank you

In what format are the dates you have? Post a sample if you like.

php online tutorials www.w3schools.com

Hello,

The date format is dd-mm-yyyy (30-11-2010).7


$dateArr  = explode("-","30-11-2010");
  $endDate=mktime(0,0,0,$dateArr[1],$dateArr[0],$dateArr[2]);
  if ($endDate > time()){
      $X = "RED";
  }elseif(($endDate - (time() + (7 * 86400)) <=0 )){
      $X = "YELLOW";
 }						


If that works for you then great, well done.

If you are dealing with a lot of dates in that dd mm yyyy format you might want to create a simple function which does that for you, which might help keep your code tidier.

If that incoming date is from direct user input, you might also want to consider using [fphp]checkdate[/fphp] to verify it is a real date (as opposed to say, 31-02-2011), and you could put that in the function too.

If you initialised $X with


$X = null ;

when you came to use $X you could then do so like this, say as an piece of inline css in a html tag:


if( $X ) echo "style='color:$X'" ;