gpt4 book ai didi

php - 在 PHP 中模拟 "do not disturb"功能

转载 作者:可可西里 更新时间:2023-11-01 00:20:13 24 4
gpt4 key购买 nike

我想每天在两个用户指定的时间之间进行检查,而不是运行某些函数调用(即“请勿打扰”)。

例如,用户在晚上 10:00 到早上 6:00(第二天)之间设置“请勿打扰”时间段。

仅供引用,最终用户没有指定日期/日期,只有时间。这将每周 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');

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

如果每天都一样,你可以这样做:

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

关于php - 在 PHP 中模拟 "do not disturb"功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28753316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com