PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

[back]WB01624_.gif (281 bytes) 12/19/99  "Changing Background Colors Based On Day Of Week" WB01626_.gif (272 bytes)[next]

Due to a suggestion from Jeff "Furball" (thanks) over at the tech board at Top Niche, I have added navigation links through this diary. If you have graphics turned off, I am also including text navigation links. Now back to my thoughts about changing HTML content based on the day of the week. If you review the date codes from 12/17/99 or the PHP user manual we will need to use an identifier for each day in the code used to change the HTML content based on the day of the week.

l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"

Let's do a little planning and figure out what we're going to do. I'd like to have the background color (white currently) for my diary entry pages be different based on the current day of the week - this will make it a little more visually interesting than plain old white pages. I will need 7 different colors, of course. To do this in HTML we use the following code:

<body bgcolor="#FEF0C5"> is light brown - Sunday
<
body bgcolor="#FFFFFF"> is white - I will use this if the current day you view is Monday
<body bgcolor="#FBFFC4"> is light yellow - Tuesday
<body bgcolor="#FFE0DD"> is pinkish - Wednesday
<body bgcolor="#E6EDFF"> is light blue - Thursday
<body bgcolor="#E9FFE6"> is light green - Friday
<body bgcolor="#F0F4F1">  is gray - Saturday

Now we can use the if statement I discussed at the end of 12/18/99. Here's the PHP and HTML code I'm gonna try:

<html>
<
head>
<title>Background Colors change based on the day of the week</title>
</head>

<?
$today = date("l");
print("$today");
  if($today == "Sunday")
  {
     $bgcolor = "#FEF0C5";
  }
  elseif($today == "Monday")
  {
     $bgcolor = "#FFFFFF";
  }
  elseif($today == "Tuesday")
  {
     $bgcolor = "#FBFFC4";
  }
  elseif($today == "Wednesday")
  {
     $bgcolor = "#FFE0DD";
  }
  elseif($today == "Thursday")
  {
     $bgcolor = "#E6EDFF";
  }
  elseif($today == "Friday")
  {
     $bgcolor = "#E9FFE6";
  }
  else
  {
     // Since it is not any of the days above it must be Saturday
     $bgcolor = "#F0F4F1";
  }
print("<body bgcolor=\"$bgcolor\">\n");
?>

There are a lot of things worth noting in the code above, but let's make sure it works first:

example #5 change the page background based on date using if elseif and else statement

It works, but 34 lines of code to do this seems excessive to me, we can prune this down.  But before we do, it's worth explaining a few things. There is only the initial if and then each subsequent elseif follows until the last item and that is an else. Whatever you want to do if the condition is true goes between the brackets.

Ok how do we get this code down to something a little more reasonable in size? One thing to remember when assembling our if statement is that we don't need to use the textual date code "Friday, Saturday, etc". We could also use:

w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)

In fact using the w date code above has advantages and can shorten the code above considerably. We can now make the 7 different background colors into values inside an array and based on the index number in the array, call the appropriate background color that corresponds to the day of the week. Therefore we don't even need to use an if statement!  Notice how Sunday starts as number zero, or the left most part of the array and 6 Sunday is the last item in the array. All arrays start numbering at zero, not one, so keep this in mind when dealing with arrays. Now if $today == 3 that means the day is Wednesday, 4 is Thursday, 1 is Monday, etc. To access an item in the array you place the index number of the item in the array inside brackets $arrayname[indexnumbergoeshere].

Let's replace the PHP code above with the following more streamlined code:

<?
$today = date("w");
$bgcolor = array(
           "#FEF0C5", "#FFFFFF", "#FBFFC4", "#FFE0DD",
           "#E6EDFF", "#E9FFE6", "#F0F4F1"
           );
?>
<body bgcolor="<?print("$bgcolor[$today]");?>">

example #6 change background color based on day of the week using array (streamlined code!)

Cool! From 34 to 8 lines of code. Now this should give you a little idea of how arrays in PHP work, it was a good exercise for me as well, since I wanted to have a dynamic PHP background :)

[back]WB01624_.gif (281 bytes) 12/19/99  "Changing Background Colors Based On Day Of Week" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com