Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a greeting based on user's time (Good morning, good afternoon…)

Tags:

Can anyone extrapolate on how to implement a basic "good evening" or "good morning" based on the user's time setting?

Perhaps PHP will fetch the server time, but I'm looking to greet the site visitor with a time-based appropriate greeting that considers their time of day.

E.G.: good morning, good night, good afternoon.

like image 780
Ddoug Avatar asked Feb 02 '10 07:02

Ddoug


People also ask

What do you say greetings based on the time?

The greetings change depending on the time of the day. For example, “Good morning” is generally used from 5:00 a.m. to 12:00 p.m. whereas “Good afternoon” time is from 12:00 p.m. to 6:00 p.m. “Good evening” is often used after 6 p.m. or when the sun goes down. Keep in mind that “Goodnight” is not a salutation.

Why in business greeting is always good morning?

It's polite Saying 'good morning' to people is polite and such courteous greeting is as basic as 'please' and 'thank you' in our daily lives. When you say 'good morning', you are not only greeting the person but also wishing them well for the day.

Is good morning a form of greeting?

Good morning is defined as a polite greeting or farewell that you say to someone in the early hours of the day. Good morning is an example of something you say to someone when you see him for the first time at 9 AM. Used as a greeting when meeting somebody for the first time in the morning.


1 Answers

Base it on .getHours() of the date object. Using javascript's Date object will automatically use the user's local time, rather than the server-time:

var now = new Date(); alert( now.getHours() ); 

A couple conditional checks, and you're in business. For instance, the following is a very simple and easy-to-understand example:

var now = new Date(); var hrs = now.getHours(); var msg = "";  if (hrs >  0) msg = "Mornin' Sunshine!"; // REALLY early if (hrs >  6) msg = "Good morning";      // After 6am if (hrs > 12) msg = "Good afternoon";    // After 12pm if (hrs > 17) msg = "Good evening";      // After 5pm if (hrs > 22) msg = "Go to bed!";        // After 10pm  alert(msg); 

It's currently 2:56am here, so I see "Mornin' Sunshine!" when I run this. You can test your own local time with this online demo: http://jsbin.com/aguyo3/edit

like image 172
Sampson Avatar answered Oct 27 '22 06:10

Sampson