PDA

View Full Version : How to display Morning, Afternoon or Evening with PHP


tux
04-27-2007, 10:20 PM
I have been asked to display Good Morning, Afternoon or Evening on the welcome page of a website depending on when the user views the page. How would I do this in PHP. It will be part of the welcome text so will need to change on the fly depending on the time day. How can this be done?

Thanks, Paul

davidj
04-28-2007, 04:20 PM
ok

here is the solution...

there is a neat function called date() in php. Not only can it perform date calculation it can harness time like dr who

date(); requires an argument to tell it what part of date we want to return so the argument we are going to use today is H

H = hours in 24 hr clock format
h = hours in 12 hr clock format

so to get the current hour we are going to use date("H");

ok lets look at the function i wrote...


function welcome(){

if(date("H") < 12){

return "good morning";

}elseif(date("H") > 11 && date("H") < 18){

return "good afternoon";

}elseif(date("H") > 17){

return "good evening";

}

}

as you can see there is an IF statement which is performing the simple condition but as i am using a function i am returning the result instead of echoing . Let me explain...

create a blank page and call it welcome.php

in welcome.php add your <?php tags as normal then paste the function into that file and save it off

in the page / pages you want to display the message just include the file at the top of every page
include("welcome.php");
then to fire the function and display the result just do the following where you want to display the message...
<?php echo welcome(); ?>

tux
04-28-2007, 08:17 PM
Thanks David,

You make it all so simple to understand.

Glad you have calmed down. What happened to the other posts??

davidj
04-28-2007, 08:18 PM
i sent them away with my anger

i am at peace with the world once again

davidj
04-28-2007, 08:28 PM
the other way is to pass the time into the function as a value


function welcome($H){

if($H < 12){

return "good morning";

}elseif($H > 11 && $H < 18){

return "good afternoon";

}elseif($H > 17){

return "good evening";

}

}

as you can see i changed the date() function to a variable

then in the function call i can pass the date() value through as an argument in the function call

<?php echo welcome(date("H")); ?>

tux
04-28-2007, 08:47 PM
The first way seems alot easier.

In the second method does that mean I would have to declare the variable at the start of the code. If so what would the $H = ????

davidj
04-29-2007, 09:27 AM
in PHP you dont have to declare variable

you can just create them and assign a value when you need them

no decleration needed

you have this function call...


welcome(date("H"));

date("H"); is parsed as a value so really you are calling this...

welcome(11); //<< the number is obviously dynamic but this is whats being passed really

then you have your function...

function welcome($H){ //<< $H variable is your catchers mit which catches the argument value from the call so in this example $H is being set with 11

if($H < 12){

return "good morning";

}elseif($H > 11 && $H < 18){

return "good afternoon";

}elseif($H > 17){

return "good evening";

}

}

remember when programming there are 100000 ways of doing the same thing. Your job is to come up with the most economical and clever way possible. Look at someone picking this up after you. Would they be able to follow your code?

Additional...
Note: comment everything you do!