PHP: Custom Greetings
Please subscribe to ProTycoon.com via the RSS Feed or Via Email.
Do you display the time on your website and want to add a small greating next to it? Now you can with this simple tutorial. We will take the current time and display a greating depending on the time of day.
I have seen many sites that display the time on their website, but few display a personal greating, this small piece of code adds a little extra touch to impress your websites visitors.
The first thing that we need to do is to output the time:
echo date("g:ia");
$time = date ("H");
The second part sets the variable $time as a 24 hour time format.
Now we move on to the main part, the custom greating. This is the code that does the job:
if ($time < 12) {
echo "Good Morning ";
} elseif ($time < 18) {
echo "Good Afternoon ";
} elseif ($time < 24) {
echo "Good Evening ";
}
The above code uses simple if statements to check the current time and display a message depending on that time. If the time is less than 12 then it displays the message “Good Morning”, if the time is less than 18 (6pm) then the message “Good Afternoon” is displayed and finally if the time is less than 24 then the message “Good Evening” is displayed.
You can now add this nice little feature to your website.
Here is the code in full:
<?php
echo date("g:ia");
$time = date ("H");
if else ($time < 12) {
echo "Good Morning ";
} elseif ($time < 18) {
echo "Good Afternoon ";
} elseif ($time < 24) {
echo "Good Evening ";
}
?>












