Php 基于用户设置问候语';时间(早上好,下午好…;)

Php 基于用户设置问候语';时间(早上好,下午好…;),php,javascript,jquery,Php,Javascript,Jquery,谁能根据用户的时间设置推断出如何实现基本的“晚上好”或“早上好” 也许PHP将获取服务器时间,但我希望用基于时间的适当问候语来问候站点访问者,并考虑他们一天中的时间 早上好,晚上好,下午好 基于日期对象的.getHours()。使用javascript的日期对象将自动使用用户的本地时间,而不是服务器时间: var now = new Date(); alert( now.getHours() ); 几张有条件的支票,你就可以做生意了。例如,以下是一个非常简单易懂的示例: var now = n

谁能根据用户的时间设置推断出如何实现基本的“晚上好”或“早上好”

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

早上好,晚上好,下午好

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

var now = new Date();
alert( now.getHours() );
几张有条件的支票,你就可以做生意了。例如,以下是一个非常简单易懂的示例:

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分,所以当我运行这个程序时,我看到了“晨曦”。您可以使用此在线演示测试自己的本地时间:


$hour=日期('H');
如果($hour>=20){
$hellives=“晚安”;
}elseif($h>17){
$hellives=“晚上好”;
}elseif($hour>11){
$hellives=“下午好”;
}elseif(小时<12){
$hellives=“早上好”;
}
回音$问候;
const now=new Date().getHours();
开关(真){
case(now12&&now=16&&now=20&&now
函数问候语{
$hour=日期('H');
如果($hour>=18){
$greeting=“晚上好”;
}其他(小时>=12){
$greeting=“下午好”;
}elseif(小时<12){
$greeting=“早上好”;
}
返回$greeting;
}

请记住,如果无法检测到时间或禁用Javascript(例如预设
msg='Welcome'
),则需要进行回退。禁用Javascript时,您不需要
msg='Welcome'
,因为没有javascript@Gordon当前位置正确。这个例子绝不仅仅是一个例子。而且,我认为你设置一个开关/案例,使其在满足第一个条件后能够断开。没有理由检查hrs是否大于12,例如,当它不大于6时。但这是一个很好的例子。@Natrium在无法检测时间或禁用Javascript时。在后一种情况下,您必须在其他地方编写Welcome,假定为默认值为问候语保留的跨距中的innerHTML。您可能希望检查最后一个条件-是否可以
date('G')
同时为19或更多和4或更少?:d最后一个条件是确定的,它检查date('G')是否大于或等于19或(即双精度|)小于或等于4,它不使用and(&&&&)
<?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";
}
?>
<?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";
    }
    ?>
$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;
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;}
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;
}