In 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
Related Articles
- 3 Customer Retention Secrets!1) Give Unexpected Gifts Give your customers more than they expect. Send thank you gifts to lifetime customers. E-mail them online greeting cards on holidays or birth...
- Using SOAP in PHPImplementing webservice servers and clients using SOAP and PHP. This paper has examples and explanations regarding implimenting effective webservices.
- How Important it is to have a Professional Logo Design for Your Business ?The article discusses about the importance of having a professionally designed logo for any business, points out the ineffectiveness of an amateur design and the impact of a professionally designed logo on the customers.
- How To Get Profits From Your '404 Page Not Found' File?Page Not Found on this Server. Check the URL and try again. Or Refresh the page...
This is the annoying error that we will come across every time we surf the web pages. Internet will be changing every minute and day. The web sites that were here are not found next day... - Quick And Easy Custom 404 PagesBy design, the server that hosts your web site delivers a 404 Error / Page Not Found when a surfer clicks on a link that the server cannot find or something else unexpected happens. The most common error page is the HTTP 404 Not found: The requested URL/whatever.html was not found on this server......
- The Secret Of Custom Error PagesJust about every website has a broken link here or there that no longer connects to the page specified in the URL. These broken links loose you visitors and cost you money. Often what happens is you delete or move a page, and then forget to update some of the other pages on your site that lin...
- Pop UpsJavaScript popups are very useful, allowing you to provide additional information or functionality on a page without navigating away from it. JavaScript popups are very easy to implement, and you can customize them to some extent.
- Hiring A Custom Website DesignerHiring someone to build or promote your website costs considerably more than building it yourself, but it can be a good way to quickly get your site online and start doing business, especially if the site you intend to build is relatively complex or you need to implement an e-commerce solution...
- Error PagesThe standard error pages can be very annoying. When you move a page in your site, visitors have no way of finding where you moved it to in the error page. With custom error pages you can get cool error pages that have the same layout as your site and get rid of the standard ones and replace them wit...
- Jquery Fade In.Fade OutIn this tutorial Ill show you some simple effects you can use to spice up your websites using jquery.
