我想每天在两个用户指定的时间之间进行检查,而不是运行一些函数调用(即"请勿打扰").
例如,用户在晚上10点到早上6点(下一天)之间设置"请勿打扰"时间块.
仅供参考,最终用户仅指定日期/日期.这将每周7天,每天持续运行.
所以在晚上10点到6点(第二天)之间,任何函数调用都会被忽略.这是我到目前为止所写的内容:
$now = time(); // or $now = strtotime('11:00pm'); to simulate time to test
$start = strtotime('10:00pm');
$end = strtotime('6:00am +1 day');
// alternative time block
//$start = strtotime('10:00am');
//$end = strtotime('11:00am');
//debug
//echo date('r', $now) . '<br>' . date('r', $start) . '<br>' . date('r', $end) . '<br><br>';
if($start > $now || $now > $end) {
echo 'disturb';
} else {
echo 'do not disturb';
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,因为一旦你到达午夜,这是新的一天,但$end变量已经是前一天了.
我试着把它放在后面一天,但问题是$end最终的价值低于价值$start,这是不正确的.
$now每当时间到达午夜时,我也尝试在变量中添加一天,但问题是,如果时间$start和$end天数在同一天内怎么办?
我在这里错过了什么?
显然你正在尝试在这里构建某种日历功能.
如果您使用strtotime('10:00pm');此选项将更改为午夜后第二天的时间戳.
所以你需要给变量一个日期
$start = strtotime('2015-02-26 10:00pm');
$end = strtotime('2015-02-27 6:00am');
Run Code Online (Sandbox Code Playgroud)
不确定如何存储这些时间块,但理想情况下它们将存储在数据库表中.
如果你每天都做同样的事情:
$now = time(); // or $now = strtotime('11:00pm'); to simulate time to test
$start = strtotime('10:00pm');
$end = strtotime('6:00am'); // without the +1 day
if($start > $end) {
if($start > $now && $now > $end) {
echo 'disturb';
} else {
echo 'do not disturb';
}
}else{
if($now < $start || $now > $end) {
echo 'disturb';
} else {
echo 'do not disturb';
}
}
Run Code Online (Sandbox Code Playgroud)