【问题标题】:Check if current time is between two times, with the possibility of lapping days检查当前时间是否在两次之间,有可能是重叠天
【发布时间】:2013-06-13 04:11:31
【问题描述】:

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

举个例子: 时间段从当天晚上 10:30 开始,到第二天下午 4:00 结束。如果当前时间在晚上 10:30 到晚上 11:59:59 之间,则提交将被分配到该时间段。但是,如果当前时间在上午 12:00 到下午 4:00 之间,则会跳过该时间段。

这是我目前所拥有的:

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

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

我知道这与以下事实有关:如果是新的一天,那么 $fromstrtotime 结果将在当天晚些时候出现。因此,它不是检查昨天晚上 10:30,而是检查今晚晚上 10:30。

我的问题是,如果需要,我似乎无法将$from 时间切换到前一天,就像我将$to 时间强制切换到第二天一样。

【问题讨论】:

  • 如果日期很重要,那为什么不把它加到你的时间里呢?
  • 时代是动态的。时隙存储在数据库中,管理员可以自行更改。我们使用它来拆分提交内容,以便审核提交内容的人有相同的工作量。

标签: php time compare strtotime


【解决方案1】:
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 如果阈值之后

希望它能满足您的需求。

【讨论】:

    【解决方案2】:

    这比您预期的要容易得多。假设您有三个时间,t1t2tn,分别代表从、到和用户时间。将这些时间视为六位数字(从 000000 到 235959)并检查:

    • 如果t1t2 出现在午夜边界的同一侧
      • 检查tn 是否介于t1t2 之间
    • 其他
      • 检查tn 是否不在t2t1 之间

    代码和测试:

    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
    

    【讨论】:

    • 非常感谢,这正是我想要的。我什至没有想过将时间转换为整数并将它们作为普通数字进行比较。我想我应该在午夜前睡觉,而不是担心是否是午夜。
    • 非常感谢。我厌倦了几天找到这个解决方案。 Php 在时间管理方面真的很糟糕。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2018-11-16
    • 2015-06-16
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    相关资源
    最近更新 更多