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 
 
//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"; 
 
?>

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

alc6379 commented: great tutorial! +5

Wo ! Thnx that was easy :)

Is there any way to have the greeting displayed in the VBulletin forum front page? :confused:

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 :cool:

Sarah

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 !

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 ?

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

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

je voudrai savoir quelque chose, j aimerai pouvoir accueillir mes member avec un tell message, mais ce message dirai quelque chose comme Bonjour Pierre et il apparaitrai dans ma chat room, cependant voila je suis nulle en php et tout autre language informatique d'ailleur. Si vs pourriez m'aider cela serait super mon site et le suivant
<snipped> et vous pouvez me contacter en me laissant un message privée dans le site Je vous en remercie d'avance

Emilie

I have no idea as to why i wrote the post in french!

anyhow here is what I am trying to do and I have to say without any type of success whatsoever! I have to mention that anything related to programing goes way over my head!

I have a website which has its own chatroom, and what I am trying to do is that when a member logs in, there is an automated greeting like hello [username] I know that the site is writen in PHP and that there will be some sql involved but I have no idea so if anyone could help me here I would send that person the relevant files and if that person is a chess player I ll even give him/her a Diamond membership. The adress of the site is <snipped> u can either contact me via contact the webmaster link located on the index.php page or you can create an account and send me a PM vith the private message feature. my username on the playground is Emilie. I look forward tio hearing from you Emilie

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.