TinyPortal

Development => Block Codes => Topic started by: evulness on April 11, 2008, 09:12:18 PM

Title: greet visitor by time of day
Post by: evulness on April 11, 2008, 09:12:18 PM
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'];
}
Title: Re: greet visitor by time of day
Post by: 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'];
}
Title: Re: greet visitor by time of day
Post by: platinumUWE on April 12, 2008, 02:18:44 AM
what kind of block does this require ty
Title: Re: greet visitor by time of day
Post by: JPDeni on April 12, 2008, 02:45:17 AM
Either one of these are php blocks.

Most things you see in here are for php blocks.
Title: Re: greet visitor by time of day
Post by: platinumUWE on April 12, 2008, 02:46:28 AM
ty very much JDpeni for your advice appeciate it thankyou.
Title: Re: greet visitor by time of day
Post by: troll on April 12, 2008, 07:08:39 PM
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'];
}

Title: Re: greet visitor by time of day
Post by: JPDeni on April 12, 2008, 07:21:59 PM
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.
Title: Re: greet visitor by time of day
Post by: troll on April 12, 2008, 07:29:04 PM
Thanks JPDeni,

I have got it working now, it was taking a while and now Im really happy :)
Title: Re: greet visitor by time of day
Post by: troll on April 12, 2008, 07:39:31 PM
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?

Title: Re: greet visitor by time of day
Post by: JPDeni on April 12, 2008, 07:44:51 PM
QuoteDoes this work on the forum time not the user time?

It works on the server time.
Title: Re: greet visitor by time of day
Post by: troll on April 12, 2008, 07:51:33 PM
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?
Title: Re: greet visitor by time of day
Post by: JPDeni on April 12, 2008, 07:57:38 PM
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.
Title: Re: greet visitor by time of day
Post by: troll on April 12, 2008, 08:00:07 PM
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!!!
Title: Re: greet visitor by time of day
Post by: JPDeni on April 12, 2008, 08:12:30 PM
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
Title: Re: greet visitor by time of day
Post by: evulness on April 14, 2008, 06:46:07 PM
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.


Title: Re: greet visitor by time of day
Post by: JPDeni on April 14, 2008, 06:47:04 PM
http://www.tinyportal.net/index.php?topic=9940.0