If statement changing colors

I want the background color to change based on the if statement. For example in the morning I want the color to be blue. How would I do this. I have googled this and everything I find is very confusing. Please help me and explain what you did. Remember when someone is learning do not assume they know what you are talking about.

To often books want to show you just a piece of code and they leave to much out for those of us trying to learn.

<?php $hour = date("H", strtotime("now")); if ($hour >= "5" and $hour < "12" ) echo "Good Morning"; elseif ($hour >=12 and $hour < 18 ) echo "Good Afternoon"; elseif ($hour >=18 and $hour < 22 ) echo "Good Night"; elseif ($hour >=22 and $hour < 24 ) echo "Hey Night Owl"; else echo " Hey Night Owl"; ?>

Before your tag do an If check similar to the one your using and then save a variable $color as whatever color instead of echoing something. Then when you do ur body tag use:

Not sure what you mean here is what I am trying to do and for some reason it is now working.

= "5" and $hour < "12" )

echo “Good Morning $bgcolor[0]” ;

elseif ($hour >= “12” and $hour < “18”)
echo "Good Afternoon “;
elseif ($hour >=18 and $hour < 22 )
echo “Good Night”;
elseif ($hour >=22 and $hour < 24 )
echo “Hey Night Owl”;
else
echo " Hey Night Owl”;
?>

<!DOCTYPE html PUBLIC " -// W3C // DTD XHTML 1.0 STRICT //EN"
"http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>      </title>
</head>
<?php
$hour = date("H", strtotime("now"));
if ($hour >= "5" and $hour < "12" ) { $color = "red"; }
elseif ($hour >=12 and $hour < 18 ) { $color = "lightblue"; }
elseif ($hour >=18 and  $hour < 22 ) { $color = "green"; }
elseif ($hour >=22 and $hour < 24 ) { $color = "yellow"; }
else { $color = $yellow"; }
?>
<body style="font-family: forte; font-style: italic; color: olive; font-size: 16pt; font-weight; bold;" bgcolor="<? Echo $color; ?>">
   
<?php
$hour = date("H", strtotime("now"));
if ($hour >= "5" and $hour < "12" ) { echo "Good Morning"; }
elseif ($hour >=12 and $hour < 18 ) { echo "Good Afternoon"; }
elseif ($hour >=18 and  $hour < 22 ) { echo  "Good Night"; }
elseif ($hour >=22 and $hour < 24 ) { echo "Hey Night Owl"; }
else { echo " Hey Night Owl"; }
?>
</body>
</html>

Try this

Karma:

That worked once I fixed your typo. Thanks for the help. Php seems to be really touchy with syntax.

Yeah it is. I never even noticed i used $ instead of " there. Why did you say Karma:

Hi KaythXC,

Look at your Karma meter under your name to the left of your post.

Cheers!

Yeah I noticed that but it was already at +1/-0 before the post saying karma: was posted.

RathXC,

He could have increased your karma before he posted. Anyone can up/down your karma.

Cheers.

I am sorry. I just glanced to the side and thought that was your name. I did not notice where this website puts your name. I do not even know how to show that this problem has been solved. I am new to the website and it does not seem very user friendly.

Anyway I did not notice the typo you were talking about. The one that stopped the program from working was
else { $color = $yellow"; } You forgot the quotes around yellow.

Sponsor our Newsletter | Privacy Policy | Terms of Service