php早午晚问候语_根据用户的时间设置问候语(早上好,下午好......)

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值