加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在PHP中模拟“请勿打扰”功能

发布时间:2020-12-13 21:53:11 所属栏目:PHP教程 来源:网络整理
导读:我想每天在两个用户指定的时间之间进行检查,而不是运行一些函数调用(即“请勿打扰”). 例如,用户在晚上10点到早上6点(下一天)之间设置“请勿打扰”时间块. 仅供参考,最终用户不会指定日期/日期.这将每周7天,每天持续运行. 所以在晚上10点到6点(第二天)之间,
我想每天在两个用户指定的时间之间进行检查,而不是运行一些函数调用(即“请勿打扰”).

例如,用户在晚上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';
}

但这似乎不起作用,因为一旦你到达午夜,这是新的一天,但$end变量已经是前一天了.

我试着把它放在后面一天,但问题是$end的值最终低于$start的值,这是不正确的.

每当时间到达午夜时我也尝试在$now变量中添加一天,但问题是,如果$start和$end时间是在同一天内怎么办?

我在这里错过了什么?

解决方法

显然你正在尝试在这里构建某种日历功能.

如果你使用strtotime(’10:00pm’);这将改为午夜后第二天的时间戳.

所以你需要给变量一个日期

$start = strtotime('2015-02-26 10:00pm');
 $end = strtotime('2015-02-27 6:00am');

不确定如何存储这些时间块,但理想情况下它们将存储在数据库表中.

如果你每天都做同样的事情:

$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';
   }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读