Her

Home Web Programming PHP Custom Greeting

Custom Greeting

Author: Nathanael Mitchell Author's URL: http://www.stilisticdev.net More by this author

Custom GreetingIn the last tutorial we learned how to display the time using php. Now this is cool and all but lets get even cooler and learn how to put that time to work and display custom greeting depending on the time of day.

This is pretty easy also, we'll us the if and elseif statement to get this little thing working. First I'll give you all the code, then we'll breaking it down.


<?

print date("g:ia");

$date = date ("H");

if ($date < 6) {
echo "Up Late Aren't We ";
} elseif ($date < 7) {
echo "Are you the early bird ";
} elseif ($date < 12) {
echo "Good Morning ";
} elseif ($date < 18) {
echo "Good Afternoon ";
} elseif ($date < 22) {
echo "Good Evening ";
} elseif ($date < 24) {
echo "Souldn't You Be Going To Bed Soon? ";
}

?>

Not so hard huh? Now for a quick breakdown of this. First we have:

print date("g:ia");

All this does is displays the time (for more about displaying the time and date, you can read my time and date tutorial.

Next we have:

$date = date ("H");

What this does is creates the variable $date. It says to use $date as the php date funcion. This will say that $date is = to the current hour in 24 hour format.

Now to the fun stuff. This is the part that actually displays the custom greeting:

if ($date < 6) {
echo "Up Late Aren't We ";
} elseif ($date < 7) {
echo "Are you the early bird ";
} elseif ($date < 12) {
echo "Good Morning ";
} elseif ($date < 18) {
echo "Good Afternoon ";
} elseif ($date < 22) {
echo "Good Evening ";
} elseif ($date < 24) {
echo "Souldn't You Be Going To Bed Soon? ";
}

The first line says that if the $date (hour) is less than 6 (6:00am) to display "Up Late Aren't We".

Now Jumping to the last line. This say if the $date (hour) is less than 24 (12:00am) to display "Souldn't You Be Going To Bed Soon?"

This means that if it is between 12:00am and 5:59am it will display "Up Late Aren't We"

Here's a little break down of the different greeting and times they will be displayed.

12:00am - 5:59am will display "Up Late Aren't We"
6:00am - 6:59am will display "Are you the early bird"
7:00 - 11:59am will display "Good Morning"
12:00pm - 5:59pm will display "Good Afternoon"
6:00pm - 9:59pm will display "Good Evening"
10:00pm - 11:59pm will display "Souldn't You Be Going To Bed Soon?"

So there you have it. This is fairly easy and give a nice effect. There are tons of different ways to use this, so let your imaginations run wild :D