PHP:在特定日期和时间显示消息

时间:2014-12-01 10:27:59

标签: php date time message

我试图让脚本在特定的日期和时间显示特定的消息。我已经让脚本在特定时间和特定时间显示,但不是两者合并。

如何让脚本在星期一下午3点到4点之间显示特定消息?

看看我到目前为止制作的剧本;

<?php
//Messages
$afternoon = "It's afternoon";
$evening = "It's evening";
$night = "It's night.";
$morning = "It's morning!";
$friday = "Soon weekend!";

//Get time
$current_time = date(G);
//Get day
$current_day = date(l);

//12PM - 4PM
if ($current_time >= 12 && $current_time <= 16) {
echo $afternoon;
}
//5PM - 11PM
elseif ($current_time >= 17 && $current_time <= 24) {
echo $evening;
}
//00AM - 05AM
elseif ($current_time >= 1 && $current_time <= 5) {
echo $night;
}
//6AM - 11AM
elseif ($current_time >= 6 && $current_time <= 11) {
echo $morning;
}

//Friday, display message
if ($current_day == "Friday") {
echo $friday;
}
?>

我感谢任何帮助!

3 个答案:

答案 0 :(得分:3)

if ($current_day == "Monday") {
    if ($current_time >= 15 && $current_time <= 16) {
        echo "It's Monday and the time is between 3PM and 4PM.";
    }
}

答案 1 :(得分:3)

if($current_day == "Monday" && $current_time >= 3 && $current_time <= 4){
   //Monday text
}

答案 2 :(得分:0)

试试这个:
将标志设置为下午和之后的标志,如果日期是星期一,则根据需要设置消息。

$afternoonFlag = False;
if ($current_time >= 12 && $current_time <= 16) {
    echo $afternoon;
    $afternoonFlag = True;
}
if ($current_day == "Monday" && $afternoonFlag) {
    echo "Your Message";
}