Hamza Abd Elwahab
Hamza Abd Elwahab

Reputation: 153

Create A PHP Variable Which Changes The Color Of The Background Weekly

I would like to create a PHP variable which I can echo to change the color of the background weekly.

Here is what I had reached so far

<?php 
    // set the default timezone to use. Available since PHP 5.1
    date_default_timezone_set('EST');
    $today = date("l");
    if($today == "Sunday") 
        {
            $color = "#FEF0C5";
        }
    elseif($today == "Monday")
        {
            $color = "#FFFFFF";
        } 
    elseif($today == "Tuesday") 
        {
            $color = "#000000";
        } 
    elseif($today == "Wednesday")
        {
            $color = "#FFE0DD";
        } 
    elseif($today == "Thursday")
        {
            $color = "#E6EDFF";
        } 
    elseif($today == "Friday") 
        {
            $color = "#E9FFE6";
        } 
    else 
        {
    // Since it is not any of the days above it must be Saturday
            $color = "#F0F4F1";
        }
    print("<body bgcolor=\"$color\">\n"); 
?>

I only managed to make the colors change daily, but i don't know how can I make the colors change weekly instead.

The second thing is that I need to make the colors of each first and last day of the month the color should be pink.

Any help would be very very much appreciated!

Upvotes: 0

Views: 878

Answers (3)

Bjorn Smeets
Bjorn Smeets

Reputation: 320

You can use this to change the color every week:

$today = date("W");

$today will be a value between 1 and 52, so you'll have every week of the year covered

date("t") returns the number of days of the current month

So to check if it's the first or last day, you can use this:

$LastDayOfMonth = date("Y-m-t");
$FirstDayOfMonth = date("Y-m-01");

So to put it all together, you could do it like this:

date_default_timezone_set('EST');
$today = date("W");
switch ($today) {
    case 1:
        $color = "the color you want";
        break;
    case 2:
        $color = "the color you want";
        break;
    case 3:
        $color = "the color you want";
        break;
    // All the other cases here...
}

$CurrentDate = date("Y-m-d");
$LastDayOfMonth = date("Y-m-t");
$FirstDayOfMonth = date("Y-m-1");

if ($CurrentDate == $LastDayOfMonth || $CurrentDate == $FirstDayOfMonth ) {
    $color = "the pink rgb-code";
}

Upvotes: 1

Chris Wesson
Chris Wesson

Reputation: 299

$week = date("W");//gives number of week (1-53)

then you could use a switch statement to change color for every week

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 175028

date("W") returns the number of the week inside the year (generally 1-52).

Upvotes: 2

Related Questions