php 根据一天中的时间运行代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8652502/
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-26 05:10:30  来源:igfitidea点击:

run code depending on the time of day

phpdatetime

提问by user983248

I need to echo some code or message on a page depending on the hour of the day. Just like a welcome message, "good evening", or "good afternoon"

我需要根据一天中的小时在页面上回显一些代码或消息。就像一条欢迎信息,“晚上好”或“下午好”

I don't know hot to group certain hours and assign a message to each group like

我不知道热点分组某些时间并向每个组分配一条消息,例如

from 1:00:00 pm to 4:00:00pm = "good afternoon" and from 4:00:01 to 8:00:00 = "good evening"

从 1:00:00 pm 到 4:00:00pm =“下午好”和从 4:00:01 到 8:00:00 =“晚上好”

so far I have :

到目前为止我有:

<?php
date_default_timezone_set('Ireland/Dublin');
$date = date('h:i:s A', time());
if ($date < 05:00:00 AM){
echo 'good morning';
}
?>

But I don't know how to pass the hours range an the messages.

但我不知道如何传递消息的时间范围。

回答by Sudhir Bastakoti


    <?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 Vance Lucas

I thought this thread could use a nice one-liner:

我认为这个线程可以使用一个很好的单线:

$hour = date('H');
$dayTerm = ($hour > 17) ? "Evening" : (($hour > 12) ? "Afternoon" : "Morning");
echo "Good " . $dayTerm;

If you check the highest hour first (evening), you can eliminate the range checks completely, and make a nice, more compact conditional statement.

如果您首先检查最高小时(晚上),则可以完全消除范围检查,并制作一个漂亮的、更紧凑的条件语句。

回答by Ben

$hour = date('H', time());

if( $hour > 6 && $hour <= 11) {
  echo "Good Morning";
}
else if($hour > 11 && $hour <= 16) {
  echo "Good Afternoon";
}
else if($hour > 16 && $hour <= 23) {
  echo "Good Evening";
}
else {
  echo "Why aren't you asleep?  Are you programming?";
}

...should get you started (timezone insensitive).

...应该让你开始(时区不敏感)。

回答by maxHymanie

$dt = new DateTime();

$hour = $dt->format('H');

now check this $hourin your morning , afternoon , evening or night range and accordingly give the message

现在$hour在你的早上、下午、晚上或夜间范围内检查这个,并相应地给出信息

回答by IMRAN HOSSAIN

date_default_timezone_set('Asia/Dhaka');
$time=date('Hi'); 

if (($time >= "0600") && ($time <= "1200")) {
  echo "Good Morning";
} 

elseif (($time >= "1201") && ($time <= "1600")) {
  echo "Good Afternoon";
}

elseif (($time >= "1601") && ($time <= "2100")) {
  echo "Good Evening";
}

elseif (($time >= "2101") && ($time <= "2400")) {
  echo "Good Night";
}
else{
  echo "Why aren't you asleep?  Are you programming?<br>";
}