我需要在页面上回显一些代码或消息,具体取决于一天中的小时.就像欢迎辞,"晚安"或"下午好"
我不知道如何分组特定时间并为每个组分配一条消息
从下午1:00:00到下午4:00:00 ="下午好",从4:00:01到8:00:00 ="晚上好"
到目前为止我有:
<?php
date_default_timezone_set('Ireland/Dublin');
$date = date('h:i:s A', time());
if ($date < 05:00:00 AM){
echo 'good morning';
}
?>
Run Code Online (Sandbox Code Playgroud)
但我不知道如何传递消息的小时范围.
Sud*_*oti 36
<?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";
}
?>
Van*_*cas 13
我认为这个线程可以使用一个很好的单线程:
$hour = date('H');
$dayTerm = ($hour > 17) ? "Evening" : (($hour > 12) ? "Afternoon" : "Morning");
echo "Good " . $dayTerm;
Run Code Online (Sandbox Code Playgroud)
如果您先检查最高时间(晚上),则可以完全消除范围检查,并制作一个更好,更紧凑的条件语句.
Ben*_*Ben 10
$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?";
}
Run Code Online (Sandbox Code Playgroud)
...应该让你开始(时区不敏感).