function greetUser()
{
$hour = date('G');
if($hour >= 0 && $hour < 12){
// if hour is 12am or up to 12pm
$message = "Good Morning";
}elseif($hour >= 12 && $hour < 18){
// if hour is 12pm or up to 6pm
$message = "Good Afternoon";
}else{
// else, the hour must be from 18-23
$message = "Good Evening";}
return $message;
// return our message to what is was called from.
}
$greetings = greetUser();
if($context['user']['is_logged'])
{
echo $greetings.' '.$context['user']['name'];
}else{
echo $greetings.' '.$txt['welcome_guest'];
}
Useful snippet. I tried it out for my site but just couldn't come up with it fitting in the way I wanted.
I reduced it to just a few lines (without a function call and comments) but also added punctuation after the user's name, as well as adding a line break for guests to separate the greeting from the welcome_guest text string.
$hour = date('G');
$greeting = 'Good Evening';
if($hour <= 12) $greeting = 'Good Morning'; elseif($hour <= 18) $greeting = 'Good Afternoon';
if($context['user']['is_logged'])
{
echo $greeting.' '.$context['user']['name'].'.';
}
else
{
echo $greeting.'.<br />'.$txt['welcome_guest'];
}
what kind of block does this require ty
Either one of these are php blocks.
Most things you see in here are for php blocks.
ty very much JDpeni for your advice appeciate it thankyou.
I'd like to know how to adjust this to create more timed events. Instead of having only 3 options how can I have say 6?
Quote from: Ianedres on April 11, 2008, 10:12:44 PM
Useful snippet. I tried it out for my site but just couldn't come up with it fitting in the way I wanted.
I reduced it to just a few lines (without a function call and comments) but also added punctuation after the user's name, as well as adding a line break for guests to separate the greeting from the welcome_guest text string.
$hour = date('G');
$greeting = 'Good Evening';
if($hour <= 12) $greeting = 'Good Morning'; elseif($hour <= 18) $greeting = 'Good Afternoon';
if($context['user']['is_logged'])
{
echo $greeting.' '.$context['user']['name'].'.';
}
else
{
echo $greeting.'.<br />'.$txt['welcome_guest'];
}
It's all in the lines
$greeting = 'Good Evening';
if($hour <= 12) $greeting = 'Good Morning';
elseif($hour <= 18) $greeting = 'Good Afternoon';
(I separated the lines to make them easier to read.)
What times do you want? 4am, 8am, 12noon, 4pm, 8pm, 12midnight?
Change it to
$greeting = 'greeting for after 8pm';
if($hour <= 4) $greeting = 'Greeting for midnight to 4am';
elseif($hour <= 8) $greeting = 'Greeting for 4am to 8am';
elseif($hour <= 12) $greeting = 'Greeting for 8am to noon';
elseif($hour <= 16) $greeting = 'Greeting for noon to 4pm';
elseif($hour <= 20) $greeting = 'Greeting for 4pm to 8pm';
If you want different times, change the numbers, based on a 24-hour clock.
Thanks JPDeni,
I have got it working now, it was taking a while and now Im really happy :)
Actually the problem is the code is only giving the 1 time which is from...
Does this work on the forum time not the user time? What I am wondering is if the forum time is X but the user time is Z will it default to the forum time?
QuoteDoes this work on the forum time not the user time?
It works on the server time.
Thanks again, now is there a way to change the forum default time? I just read on SMF that its based on the server time is that right?
It is based on server time, but you can alter it in the admin section.
Add this line to the start of the code:
global $user_info, $modSettings;
And change
$hour = date('G');
to
$time = time() + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600;
$hour = date('G',$time);
This will take into account any adjustments you have made to the time, as well as adjustments that an individual has made through his/her profile.
You are a clever cookie! I dont think I better mess about with all the settings though just to have a few members see it change <<-- *rolls eyes*
They can wait until it changes!!!
Well, I wrote a whole long thing that has a different greeting for every hour of the day and also greets people on holidays, birthdays and their anniversary of joining the site. Also a special greeting for someone's first day on the site. It can also have images.
http://www.tinyportal.net/index.php?topic=9940.0
This code is much simpler, though. I went a little loony with my code. :o
wow i'm glad others found this useful lol
i'm surprised no one else has posted something similar.
i liked your custom one you post, but didn't want the pictures.
http://www.tinyportal.net/index.php?topic=9940.0