TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

October 06, 2024, 07:47:29 PM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,395
  • Total Topics: 21,244
  • Online today: 162
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 67
  • Total: 67

greet visitor by time of day

Started by evulness, April 11, 2008, 09:12:18 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

evulness

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'];
}

Ianedres

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'];
}

platinumUWE

what kind of block does this require ty

JPDeni

Either one of these are php blocks.

Most things you see in here are for php blocks.

platinumUWE

ty very much JDpeni for your advice appeciate it thankyou.

troll

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'];
}


JPDeni

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.

troll

Thanks JPDeni,

I have got it working now, it was taking a while and now Im really happy :)

troll

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?


JPDeni

QuoteDoes this work on the forum time not the user time?

It works on the server time.