| | |
Greeting Your Members & Guests
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Intro
This tutorial shows how to greet your visitors depending on what time of day it is. If it's morning, it greets your visitor with "Good Morning!" If it's afternoon, it greets them with "Good Afternoon!" Magic? Noooo! Just PHP!
Getting started
We will start by looking at the entire script and then dissecting it afterward.
[PHP]<?php
//Here we define out main variables
$welcome_string="Welcome!";
$numeric_date=date("G");
//Start conditionals based on military time
if($numeric_date>=0&&$numeric_date<=11)
$welcome_string="Good Morning!";
else if($numeric_date>=12&&$numeric_date<=17)
$welcome_string="Good Afternoon!";
else if($numeric_date>=18&&$numeric_date<=23)
$welcome_string="Good Evening!";
//Display our greeting
echo "$welcome_string";
?>[/PHP]
Now, Im going to break it down to you like this:
Enjoy!:o
This tutorial shows how to greet your visitors depending on what time of day it is. If it's morning, it greets your visitor with "Good Morning!" If it's afternoon, it greets them with "Good Afternoon!" Magic? Noooo! Just PHP!
Getting started
We will start by looking at the entire script and then dissecting it afterward.
[PHP]<?php
//Here we define out main variables
$welcome_string="Welcome!";
$numeric_date=date("G");
//Start conditionals based on military time
if($numeric_date>=0&&$numeric_date<=11)
$welcome_string="Good Morning!";
else if($numeric_date>=12&&$numeric_date<=17)
$welcome_string="Good Afternoon!";
else if($numeric_date>=18&&$numeric_date<=23)
$welcome_string="Good Evening!";
//Display our greeting
echo "$welcome_string";
?>[/PHP]
Now, Im going to break it down to you like this:
•
•
•
•
In lines 4 and 5, we define the two main variables that we will be using throughout the rest of the script. We define $welcome_string as "Welcome!" just in case anything were to go wrong in the script. We define $numeric_date as date("G"). Why "G" you ask? G is the universal variable in the date() function for 24 hour time (You can find the other universal time/date variables in the time/date tutorial found here). We want the 24 hour time (military) so that we can tell what time of day it is so we can greet our viewers properly.
Now that we have grabbed our time and set it to the variable "$numeric_date", we need to set our conditions for displaying which greeting. In lines 8 and 9, we use PHP to say is the 24 hour time is greater than or equal to 0 and less than or equal to 11, modify $welcome_string to be equal to "Good Morning!." This is because any time between or during 11, it is still morning time.
Lines 10 and 11 are done the same as 8 and 9, except this time, we are aiming for times between 12 and 17, since this is the afternoon time. It also sets $welcome_string to "Good Afternoon!"
Lines 12 and 13 do the same as 8-11, except aim for times between 18 and 23 and sets $welcome_string to "Good Evening!"
Now that we have set up our conditions for displaying what greeting, we need to display our greeting. That's why we have line 12. We echo $welcome_string, which has been set by our conditions to whichever greeting is needed for the time of day.
Now that you understand what goes on in the script, put the script in its' entirety into a file of its' own named welcome_script.php. Now go to whatever place in your code that you want the greeting to be shown and include welcome_script.php. If you don't know anything about includes, check out my tutorial on includes here. If you don't want to learn about includes, or just plain don't want to include it (both of which I don't know why you wouldn't want to do), then you can just insert the script itself into the part of your website code where you want it to be displayed.
Last edited by happygeek; Oct 28th, 2006 at 11:18 am. Reason: Formatting
dynastyCODERS#1 when it comes to Programming Tutorials, Database designs and discussions, Operating Systems, you name it, check us out and drop us a line to tell us your opinions on any and everything in mind!;)
I'm no fan of javascript but this may be a valid time to use it, as javascript will use the clock on my PC and not the server - as your concept of morning may not be mine
Sarah
Sarah
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by mikeSQL
Now that you understand what goes on in the script, put the script in its' entirety into a file of its' own named welcome_script.php. Now go to whatever place in your code that you want the greeting to be shown and include welcome_script.php. If you don't know anything about includes, check out my tutorial on includes here. If you don't want to learn about includes, or just plain don't want to include it (both of which I don't know why you wouldn't want to do), then you can just insert the script itself into the part of your website code where you want it to be displayed.
I use the Etomite content management system on my web site which enables one to insert snippets of code into the database and call them on any web page (or template), and I have incorporated this coding into my site template.
For any other visitors who use Etomite here's what to do.
Copy the CODING (without the <?php and ?>) into a new snippet in site manager, naming it (say) Greeting.
Change the line
echo "$welcome_string";
to
return "$welcome_string";
and save the file.
Wherever you want the greeting to be displayed call the snippet by [[Greeting]] - and there you are !
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
Just one question.
Does this coding use the server time or the local PC time ?
I have tested it out this morning and the greeting changes at noon, but if I change my PC clock to 17:05 it does not change the greeting, and on altering the code to display $numeric_date it shows 12 ! And yes, I HAVE refreshed my browers - several times.
If it uses the server time then I can understand this as my ISP server is located in the UK, but if it is supposed to use the local PC time then I can't understand it.
Any comments or suggestions ?
Does this coding use the server time or the local PC time ?
I have tested it out this morning and the greeting changes at noon, but if I change my PC clock to 17:05 it does not change the greeting, and on altering the code to display $numeric_date it shows 12 ! And yes, I HAVE refreshed my browers - several times.
If it uses the server time then I can understand this as my ISP server is located in the UK, but if it is supposed to use the local PC time then I can't understand it.
Any comments or suggestions ?
•
•
Join Date: Jun 2005
Posts: 1
Reputation:
Solved Threads: 0
As far as I can tell this should do exactly what it says. I have ninjapad & it runs fine there. But when accessd through the web on the apache2 server with php enabled it only process to the first > in the first if statement. Any thoughts? Do i Need to change something else in the web server config? The snipet can be viewed at tamat.no-ip.org
•
•
Join Date: Jun 2005
Posts: 3
Reputation:
Solved Threads: 0
Indeed it is based on the server time, as the date() funciton is a PHP function and therefore happens at run time on the server before the page is ever delivered to the user. For a browser based local to user solution, JavaScript would make more sense.
I am now using the following :-
<!-- script to display greeting -->
<script language="JavaScript"><!--
var Greeting = "Hello. ";
var Today = new Date();
var CurrentHour = Today.getHours();
if (CurrentHour < 12) { Greeting = "Good Morning. "; }
if (CurrentHour >= 12 && CurrentHour < 17) { Greeting = "Good Afternoon. "; } if (CurrentHour >= 17) { Greeting = "Good Evening. "; } document.write(Greeting); //--></script><noscript>Hello. </noscript>
<!-- end of greeting script -->
<!-- script to display full date -->
<script language="JavaScript">
var Today = new Date();
var Days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var Months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var Year = Today.getYear();
if (Year < 1000) { Year += 1900; }
var Day = Today.getDate();
var DateEnding = "th";
if (Day == 1 || Day == 21 || Day == 31) { DateEnding = "st"; }
else if (Day == 2 || Day == 22) { DateEnding = "nd"; }
else if (Day == 3 || Day == 23) { DateEnding = "rd"; }
document.write( Days[Today.getDay()] + " " + Day + DateEnding + " " + Months[Today.getMonth()] + " " + Year );
</script>
<!-- end of date script -->
Put in the site template or page where needed, and then make any additions for displaying the time - although as it is not a continuous displat this seems rather superfluous.
Gordon
I am now using the following :-
<!-- script to display greeting -->
<script language="JavaScript"><!--
var Greeting = "Hello. ";
var Today = new Date();
var CurrentHour = Today.getHours();
if (CurrentHour < 12) { Greeting = "Good Morning. "; }
if (CurrentHour >= 12 && CurrentHour < 17) { Greeting = "Good Afternoon. "; } if (CurrentHour >= 17) { Greeting = "Good Evening. "; } document.write(Greeting); //--></script><noscript>Hello. </noscript>
<!-- end of greeting script -->
<!-- script to display full date -->
<script language="JavaScript">
var Today = new Date();
var Days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var Months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var Year = Today.getYear();
if (Year < 1000) { Year += 1900; }
var Day = Today.getDate();
var DateEnding = "th";
if (Day == 1 || Day == 21 || Day == 31) { DateEnding = "st"; }
else if (Day == 2 || Day == 22) { DateEnding = "nd"; }
else if (Day == 3 || Day == 23) { DateEnding = "rd"; }
document.write( Days[Today.getDay()] + " " + Day + DateEnding + " " + Months[Today.getMonth()] + " " + Year );
</script>
<!-- end of date script -->
Put in the site template or page where needed, and then make any additions for displaying the time - although as it is not a continuous displat this seems rather superfluous.
Gordon
Last edited by happygeek; Oct 28th, 2006 at 11:18 am. Reason: fake sig url snipped
![]() |
Similar Threads
- Prune Your Members (DaniWeb Community Feedback)
Other Threads in the PHP Forum
- Previous Thread: displaying products in categories
- Next Thread: Create Intranet Using Php
| Thread Tools | Search this Thread |
.htaccess action ajax apache api array auto beginner binary bounce broken cakephp checkbox class cms code cron curl database date display dynamic echo email error errorlog file files folder form format forms function functions google href htaccess html image include insert integration interactive ip java javascript joomla limit link login loop mail malfunctioning masterthesis menu mlm mod_rewrite multiple mysql nodes oop paypal pdf php popup problem query radio ram random recursion reference regex remote return script search server sessions sms soap source space sql syntax system table tutorial unset update upload url validation validator variable video web websitecontactform xml youtube





