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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2182511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:08:15  来源:igfitidea点击:

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

phpjavascriptjquery

提问by Ddoug

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.

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

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

EG:早上好,晚上好,下午好。

回答by Sampson

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:

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

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

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

回答by Prem Gurusamy

<?php
// I'm India so my timezone is Asia/Calcutta
date_default_timezone_set('Asia/Calcutta');

// 24-hour format of an hour without leading zeros (0 through 23)
$Hour = date('G');

if ( $Hour >= 5 && $Hour <= 11 ) {
    echo "Good Morning";
} else if ( $Hour >= 12 && $Hour <= 18 ) {
    echo "Good Afternoon";
} else if ( $Hour >= 19 || $Hour <= 4 ) {
    echo "Good Evening";
}
?>

For more information about date see function.date. I Hope this will help.

有关日期的更多信息,请参阅function.date。我希望这将有所帮助。

回答by Md. Aftab Uddin tohan

<?php
    /* This sets the $time variable to the current hour in the 24 hour clock format */
    $time = date("H");
    /* Set the $timezone variable to become the current timezone */
    $timezone = date("e");
    /* If the time is less than 1200 hours, show good morning */
    if ($time < "12") {
        echo "Good morning";
    } else
    /* If the time is grater than or equal to 1200 hours, but less than 1700 hours, so good afternoon */
    if ($time >= "12" && $time < "17") {
        echo "Good afternoon";
    } else
    /* Should the time be between or equal to 1700 and 1900 hours, show good evening */
    if ($time >= "17" && $time < "19") {
        echo "Good evening";
    } else
    /* Finally, show good night if the time is greater than or equal to 1900 hours */
    if ($time >= "19") {
        echo "Good night";
    }
    ?>

回答by RN Kushwaha

$hour      = date('H');

if ($hour >= 20) {
    $greetings = "Good Night";
} elseif ($hour > 17) {
   $greetings = "Good Evening";
} elseif ($hour > 11) {
    $greetings = "Good Afternoon";
} elseif ($hour < 12) {
   $greetings = "Good Morning";
}
echo $greetings;

回答by Ravuru Bhargav

const now = new Date().getHours();

switch (true) {
    case (now<=12): console.log("Good Morning");break;
    case (now>12 && now<16): console.log("Good Afternnon");break;
    case (now>=16 && now<20): console.log("Good Evening");break;
    case (now>=20 && now<=24): console.log("Good Night");break;}

This code is based on JavaScript, Hope this will help you.

此代码基于 JavaScript,希望对您有所帮助。

回答by Angrej Kumar

function greeting_msg() {
    $hour = date('H');
    if ($hour >= 18) {
       $greeting = "Good Evening";
    } elseif ($hour >= 12) {
        $greeting = "Good Afternoon";
    } elseif ($hour < 12) {
       $greeting = "Good Morning";
    }
    return $greeting;
}