在特定日期的特定时间显示消息

Show a message at specific times on a specific day

如何在以下内容中插入特定的分钟以显示消息:

if ($current_day == "Monday") {
if ($current_time >= 15 && $current_time <= 16) {
    echo "message";
}
}

例如,我希望 "message" 在 3:45 或 15:45 显示,而不是在第 15 到 16 小时之间显示。

我想出了如何使用 javascript:

// Sunday is 0 Monday is 1 and so on
// January is 0 February is 1 and so on

var Digital=new Date()
var month=Digital.getUTCMonth()
var day=Digital.getUTCDate()
var year=Digital.getUTCFullYear()
var hours=Digital.getUTCHours()
var minutes=Digital.getUTCMinutes()

if (month==2&&day==16&&year==2015&&hours==01&&minutes==30 || hours==01&&minutes==45 || hours==01&&minutes==47)
document.write('<b>message here</b>')
else if (month==2&&day==17&&year==2015&&hours==01&&minutes==30 || hours==01&&minutes==45 || hours==01&&minutes==47)
document.write('<b>message 2 here</b>')
else
document.write('<b></b>')

无论您在哪个时区,都能完美运行。可能有一种不那么疯狂的方法,但这就是我目前所采用的方法。

为了在上午 3:45 和下午 3:45 显示一条消息,您可以试试这个:

$current_day = date('l');
$current_time = date('G:i');

if ($current_day == "Monday") {
    if ($current_time == '03:45' || $current_time == '15:45') {
        echo "message";
    }
}

或者更简洁的方法(特别是如果您想包含更长的时间列表):

if (date('l') == "Monday") {
    if (in_array(date('G:i'), array('03:45','15:45')) {
        echo "message";
    }
}

编辑: 要指定时区,请尝试在上述任何一段代码之前插入此行:

date_default_timezone_set('America/Los_Angeles');

其中 'America/Los_Angeles' 可以是 PHP 手册这一页中代表您所需时区的任何受支持的字符串:http://php.net/manual/en/timezones.php