根据用户的时间设置问候语(早上好,下午好......) [英] Setting a greeting based on user's time (Good morning, good afternoon…)

查看:811
本文介绍了根据用户的时间设置问候语(早上好,下午好......)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以根据用户的时间设置推断如何实现基本的晚安或早上好吗?

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

也许PHP会获取服务器时间,但我希望用基于时间的适当问候语来问候网站访问者,该问候语考虑了他们的时间。

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。:早上好,晚安,下午好。

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

推荐答案

基于日期对象的 .getHours()。使用javascript的Date对象将自动使用用户的本地时间,而不是服务器时间:

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);

现在是凌晨2:56,所以我看到Mornin'Sunshine!当我跑这个。您可以使用此在线演示测试您自己的当地时间: http://jsbin.com/aguyo3/edit

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

这篇关于根据用户的时间设置问候语(早上好,下午好......)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆