检查当前时间是否在两次之间,可能有研磨天数 [英] Check if current time is between two times, with the possibility of lapping days

查看:105
本文介绍了检查当前时间是否在两次之间,可能有研磨天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受用户提交的系统,收到提交后,系统将通过所有时间段查找适当的时间段。问题是,它需要能够检查开始&



采取以下示例:
时间段从当天的晚上10:30开始,结束时间为结束时间第二天下午4:00。如果当前时间是在10:30 PM和11:59:59 PM之间,则提交将被分配到该时间段。但是,如果当前时间是在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 30:00,16:00:00)
);
foreach($ timeslots as $ slot){
if(check_time($ slot [0],$ slot [1],date(H:i:s)))
{
echotrue\\\
;
}
else
{
echofalse\\\
;
}
}

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

  false 
true

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

 <$虽然技术上介于两次之间,但是c $ c> false 
false



我知道这与如果是新的一天有关,那么 strtotime 结果为 $ from 将在当天晚些时候。因此,不是昨天检查下午10:30,它检查今晚10:30 PM。



我的问题是,我似乎没有想出一个办法 $ from 时间切换到前一天,如果需要,类似于我强制 $到时间第二天。

解决方案

这比你期望的容易得多。假设您有三次, t1 t2 tn 其分别表示从,到和用户时间。将这些时间视为六位数(从000000到235959)并检查:




  • 如果 t1 t2 存在于午夜边界的同一侧

    • 检查 tn 位于 t1 t2


  • 否则

    • 检查 tn 是否不在 t2 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),
数组(16:00:00,22:30:00,16:00:00),
数组00,22:29:59),
数组(16:00:00,22:30:00,22:30:00),
数组16:00:00,22:30:00,23:59:59),
数组(22:30:00,16:00:00,22:29 :59),
数组(22:30:00,16:00:00,22:30:00),
数组16:00:00,15:59:59),
数组(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包含$ t0:。 (check_time($ t1,$ t2,$ t0)?yes:no)。 \\\
;
}
// OUTPUT
//
// 16:00:00 - 22:30:00包含15:00:00:no
// 16 :00:00 - 22:30:00包含16:00:00:是
// 16:00:00 - 22:30:00包含22:29:59:是
// 16 :00:00 - 22:30:00包含22:30:00:否
// 16:00:00 - 22:30:00包含23:59:59:no
// 22 :30:00 - 16:00:00 contains 22:29:59:no
// 22:30:00 - 16:00:00包含22:30:00:yes
// 22 :30:00 - 16:00:00包含15:59:59:是
// 22:30:00 - 16:00:00包含16:00:00:no
// 22 :30:00 - 16:00:00包含17:00:00:no


I have a system that accepts user submissions, and upon receiving a submission the system will go through all timeslots to find the appropriate timeslot. The problem is that it needs to be able to check against the start & end times if the end time laps to the next day.

Take the following example: A timeslot begins at 10:30 PM on the current day and ends at 4:00 PM the next day. If the current time is between 10:30 PM and 11:59:59 PM, the submission will be assigned to that timeslot. However, if the current time is between 12:00 AM and 4:00 PM then it will skip the timeslot.

This is what I have so far:

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";     
        }
}

If the current time is 23:00:00, then the result would be

false
true

But if the current time is 12:00:00 then the result would be

false
false

even though it's technically between the two times.

I know it has to do with the fact that if it's a new day, then the strtotime result for $from will be later in the day. So instead of checking for 10:30 PM yesterday, it checks for 10:30 PM tonight.

My problem is that I cannot seem to come up with a way to make the $from time switch to the previous day if it needs to, similar to how I force the $to time into the next day.

解决方案

This is much easier than you expect. Assume that you have three times, t1, t2 and tn which represent from, to and user time respectively. Treat these times as six digit numbers (from 000000 to 235959) and check:

  • If t1 and t2 are present on the same side of midnight boundary
    • Check if tn lies between t1 and t2
  • Else
    • Check if tn does not lie between t2 and t1

Code and tests:

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

这篇关于检查当前时间是否在两次之间,可能有研磨天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆