Have you ever wondered how websites show a greeting according to the time? Let's find out how to do just that, using PHP. Of course, you can use other programming languages.
The PHP method of this script takes the local time zone (the one from the server), so it may be different to your visitors. This is why we show the time zone in this tutorial.
To begin with, as ever with any PHP script, we need to declare the PHP start tags.
Now, we need to set two variables. The first one $time and the second one $timezone. These will be what the script interprets.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
/* Set the $timezone variable to become the current timezone */
Now we have that step up, we need to determine our checks to find out the time and display the appropriate greeting. The first greeting will be 'good morning from $timezone'. So, let's create an IF statement to see whether the time is below 12pm and show the output.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
/* Set the $timezone variable to become the current timezone */
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning from $timezone";
}
Move on to the next page for the next steps.
So now we need to check to see if the time is in the afternoon; greater than 12pm but less than 5pm (as this would be evening). This is now an 'else if', so add the word 'else' to the line above and create a check containing an and statement. This becomes an IF AND statement, checking between a range.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
/* Set the $timezone variable to become the current timezone */
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning from $timezone";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon from $timezone";
}
Repeat this process, replacing the output and the numbers in the range to create an evening statement.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
/* Set the $timezone variable to become the current timezone */
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning from $timezone";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon from $timezone";
} else
/* Should the time be between or equal to 1700 and 1900 hours, show good evening */
if ($time >= "17" && $time < "19") {
echo "Good evening from $timezone";
} else
Move on to the final page to complete the tutorial.
We have now created everything up to the 'good night' check. All we need to do now, similarly to the code in step 1, is to check whether the time is greater than or equal to 7pm (1900 hours). Then end our PHP script.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
/* Set the $timezone variable to become the current timezone */
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning from $timezone";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon from $timezone";
} else
/* Should the time be between or equal to 1700 and 1900 hours, show good evening */
if ($time >= "17" && $time < "19") {
echo "Good evening from $timezone";
} else
/* Finally, show good night if the time is greater than or equal to 1900 hours */
if ($time >= "19") {
echo "Good night from $timezone";
}
?>
This is the final result of the tutorial.
<?php
/* This sets the $time variable to the current hour in the 24 hour clock format */
/* Set the $timezone variable to become the current timezone */
/* If the time is less than 1200 hours, show good morning */
if ($time < "12") {
echo "Good morning from $timezone";
} else
/* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
if ($time >= "12" && $time < "17") {
echo "Good afternoon from $timezone";
} else
/* Should the time be between or equal to 1700 and 1900 hours, show good evening */
if ($time >= "17" && $time < "19") {
echo "Good evening from $timezone";
} else
/* Finally, show good night if the time is greater than or equal to 1900 hours */
if ($time >= "19") {
echo "Good night from $timezone";
}
?>
This tutorial is up to date as of 04 October 09. While you're here, why not check out more tutorials? You could also subscribe to our RSS Feed, or even check out our Twitter - follow us for the latest updates.
For video tutorials, subscribe to our YouTube channel. Feel free to check out our blog for more updates.