检查当前时间是否介于两次之间,可能是天数

时间:2013-06-17 08:45:25

标签: php time compare strtotime

我有一个接受用户提交的系统,并且在收到提交后,系统将遍历所有时间段以找到适当的时间段。问题是它需要能够检查开始和放大。如果结束时间到达第二天,则结束时间。

采用以下示例: 时段从当天晚上10:30开始,到第二天下午4:00结束。如果当前时间是晚上10:30到晚上11:59:59,则提交将分配给该时间段。但是,如果当前时间是在12:00 AM到4:00 PM之间,那么它将跳过时间段。

这是我到目前为止所做的:

function check_time($from, $to, $time) {
    $time = strtotime($time);
    $from = strtotime($from);
    $to_ = strtotime($to);
    $to = $to_ <= $from ? strtotime($to . " tomorrow") : $to_;
    return ($time >= $from && $time <= $to);
}

$timeslots = array(
    array("16:00:00", "22:30:00"),
    array("22:30:00", "16:00:00")
);
foreach ($timeslots as $slot) {
    if (check_time($slot[0], $slot[1], date("H:i:s")))
        {
            echo "true\n";
        }
    else 
        {
            echo "false\n";     
        }
}

如果当前时间是23:00:00,那么结果将是

false
true

但如果当前时间是12:00:00,那么结果将是

false
false

即使技术上介于两次之间。

我知道这与以下事实有关:如果是新的一天,那么strtotime的{​​{1}}结果将在当天晚些时候结束。因此,它不会在昨天晚上10:30检查,而是在今晚10:30检查。

我的问题是,如果需要的话,我似乎无法想出让$from时间切换到前一天的方法,类似于我强迫$from时间进入下一天的时间一天。

2 个答案:

答案 0 :(得分:8)

这比你想象的要容易得多。假设您有三次t1t2tn分别代表from,to和user time。将这些时间视为六位数字(从000000到235959)并检查:

  • 如果t1t2出现在午夜边界的同一侧
    • 检查tnt1
    • 之间是否有t2
  • 否则
    • 检查tnt2
    • 之间是否有t1

代码和测试:

function check_time($t1, $t2, $tn) {
    $t1 = +str_replace(":", "", $t1);
    $t2 = +str_replace(":", "", $t2);
    $tn = +str_replace(":", "", $tn);
    if ($t2 >= $t1) {
        return $t1 <= $tn && $tn < $t2;
    } else {
        return ! ($t2 <= $tn && $tn < $t1);
    }
}
$tests = array(
    array("16:00:00", "22:30:00", "15:00:00"),
    array("16:00:00", "22:30:00", "16:00:00"),
    array("16:00:00", "22:30:00", "22:29:59"),
    array("16:00:00", "22:30:00", "22:30:00"),
    array("16:00:00", "22:30:00", "23:59:59"),
    array("22:30:00", "16:00:00", "22:29:59"),
    array("22:30:00", "16:00:00", "22:30:00"),
    array("22:30:00", "16:00:00", "15:59:59"),
    array("22:30:00", "16:00:00", "16:00:00"),
    array("22:30:00", "16:00:00", "17:00:00")
);
foreach($tests as $test) {
    list($t1, $t2, $t0) = $test;
    echo "$t1 - $t2 contains $t0: " . (check_time($t1, $t2, $t0) ? "yes" : "no") . "\n";
}
// OUTPUT
//
// 16:00:00 - 22:30:00 contains 15:00:00: no
// 16:00:00 - 22:30:00 contains 16:00:00: yes
// 16:00:00 - 22:30:00 contains 22:29:59: yes
// 16:00:00 - 22:30:00 contains 22:30:00: no
// 16:00:00 - 22:30:00 contains 23:59:59: no
// 22:30:00 - 16:00:00 contains 22:29:59: no
// 22:30:00 - 16:00:00 contains 22:30:00: yes
// 22:30:00 - 16:00:00 contains 15:59:59: yes
// 22:30:00 - 16:00:00 contains 16:00:00: no
// 22:30:00 - 16:00:00 contains 17:00:00: no

答案 1 :(得分:2)

function check_time($time, $threshold){
    $times = array(&$time, &$threshold);
    foreach($times as &$ts){
        if(!preg_match('~^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$~', $ts)){
            return false;
        }
        $ts = intval(preg_replace('~[^0-9]+~', null, $ts));
    }
    return ($time >= $threshold) ? 2 : 1;
}

var_dump(check_time(date("H:i:s"), '12:00:00'));

我作弊了!让我们遵循逻辑:

  • 如果我们删除标点符号,任何时间都可以转换为整数。
  • 我从您要测试的$time中删除了标点符号,并将其设为整数。
  • 我对$threshold做了同样的事情,这是打破插槽空间的日期。
  • 此函数只检查$time整数是在$threshold之前还是之后。
  • 它对日期,明天,时间相对性等都不关心。

如果失败(错误输入),则returns false。如果成功则返回一个整数:   - 1 如果之前阈值   - 2 如果 阈值后

希望它符合您的需求。

相关问题