Infolinks

Breaking

Adsense

Sunday, April 17, 2016

CHANGE BACKGROUND COLOR BASED ON COMPUTER DAY

Good day guys! I have another code snippet for you using the switch case statement in php. This code will get the current computer day (Monday, Tuesday, etc.) and will change the background color of the html page. Here are the colors that correspond in each day:

Monday – red
Tuesday – yellow
Wednesday – blue
Thursday – orange
Friday – green
Saturday – pink
Sunday – black

PHP Code for changing background base on day in php


<!DOCTYPE html>


<html>


<head>


            <title>Change Background</title>


</head>


<?php


            date_default_timezone_set("Asia/Manila");


            $day=date('l');


            echo "<h1>Today is $day</h1>";





            switch($day)


            {


                        case "Monday":


                        echo "<body style='background:red'";


                        break;





                        case "Tuesday":


                        echo "<body style='background:yellow'";


                        break;


                        case "Wednesday":


                        echo "<body style='background:blue'";


                        break;


                        case "Thursday":


                        echo "<body style='background:orange'";


                        break;


                        case "Friday":


                        echo "<body style='background:green'";


                        break;


                        case "Saturday":


                        echo "<body style='background:pink'";


                        break;


                        case "Sunday":


                        echo "<body style='background:black'";


                        break;


            }


?>


</body>


</html>

In the code above, I used my default timezone which is set to Asia/Manila. You can change it to your timezone so that the script will get the day based on your timezone. You can also achieve the same result using the if elseif else statement in php. I also did use color names but again it depends on you if you’ll use the same or perhaps use hexadecimal or RGB values.

Sample Output for changing background base on day in php


CHANGE BACKGROUND COLOR BASED ON COMPUTER DAY

No comments:

Post a Comment

Adbox