Greeting Your Members & Guests

Reply

Join Date: Dec 2004
Posts: 196
Reputation: mikeSQL is an unknown quantity at this point 
Solved Threads: 3
mikeSQL's Avatar
mikeSQL mikeSQL is offline Offline
Junior Poster

Greeting Your Members & Guests

 
1
  #1
Dec 28th, 2004
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:

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.
Enjoy!:o
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!;)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 37
Reputation: iwonder is an unknown quantity at this point 
Solved Threads: 0
iwonder's Avatar
iwonder iwonder is offline Offline
Light Poster

Re: Greeting Your Members & Guests

 
0
  #2
Feb 15th, 2005
Wo ! Thnx that was easy
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 2
Reputation: Dustin is an unknown quantity at this point 
Solved Threads: 0
Dustin Dustin is offline Offline
Newbie Poster

Re: Greeting Your Members & Guests

 
0
  #3
May 18th, 2005
Is there any way to have the greeting displayed in the VBulletin forum front page?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 138
Reputation: sarahk is an unknown quantity at this point 
Solved Threads: 1
sarahk's Avatar
sarahk sarahk is offline Offline
Junior Poster

Re: Greeting Your Members & Guests

 
0
  #4
May 21st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: gtatler is an unknown quantity at this point 
Solved Threads: 0
gtatler gtatler is offline Offline
Newbie Poster

Re: Greeting Your Members & Guests

 
0
  #5
Jun 15th, 2005
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.
Smashing !

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 !
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: gtatler is an unknown quantity at this point 
Solved Threads: 0
gtatler gtatler is offline Offline
Newbie Poster

Re: Greeting Your Members & Guests

 
0
  #6
Jun 15th, 2005
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 ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: T`Amat is an unknown quantity at this point 
Solved Threads: 0
T`Amat T`Amat is offline Offline
Newbie Poster

Re: Greeting Your Members & Guests

 
0
  #7
Jun 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 3
Reputation: gtatler is an unknown quantity at this point 
Solved Threads: 0
gtatler gtatler is offline Offline
Newbie Poster

Re: Greeting Your Members & Guests

 
0
  #8
Jun 15th, 2005
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
Last edited by happygeek; Oct 28th, 2006 at 11:18 am. Reason: fake sig url snipped
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC