当前时间介于时间范围之间

时间:2017-08-11 08:06:12

标签: php time range strtotime

我需要检查当前时间是否在范围

之间
       if((strtotime($s)<=strtotime($myTime)) && (strtotime($e)>=strtotime($myTime)))   
          {$open =1;}
      if($e=="00:00")                                                        
        { if((strtotime($s)<=strtotime($myTime))&& (strtotime($myTime)>=strtotime("00:00")))    
            {$open =1;}}

必须检查所有案例

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {

});

这不适用于我出错的地方

5 个答案:

答案 0 :(得分:1)

这样的事可能有用。我还没有真正测试过它。

$currentTime = time();
function in_range($what, $min, $max) {
    return $what >= $min && $what <= $max;
}

var_dump(in_range($currentTime, strtotime('08:35'), strtotime('15:30'));
// etc.

答案 1 :(得分:1)

使用DateTime对象进行比较。见http://php.net/manual/en/class.datetime.php

$cases = array(
        array(
                'start' => new DateTime("10:00:00"),
                'end' => new DateTime("12:00:00"),
        ),              
        array(
                'start' => new DateTime("10:00:00"),
                'end' => new DateTime("23:59:00"),

        ),
        array(
                'start' => new DateTime("10:00:00"),
                'end' => new DateTime("00:00:00"),

        ),
        array(
                'start' => new DateTime("23:59:00"),
                'end' => new DateTime("05:00:00"),

        ),
);

//adjust dates
$cases[2]['end']->modify('+1 day');
$cases[3]['start']->modify('-1 day');

$now = new DateTime();
foreach ($cases as $key => $value) {
    if ($now > $value['start'] && $now < $value['end']) {
        echo 'case ' . $key  . ' ok' . PHP_EOL; 
    }
    else {
        echo 'case ' . $key  . ' out of range' . PHP_EOL;
    }
}

答案 2 :(得分:0)

对吧,你身边一团糟。你想看看这是否达到你的预期产量?似乎正在进行我的本地测试。公平的代码思想,原来是一些需要考虑的边缘情况。

它的潜在部分可以重构和改进,但如果它按预期工作,我会把它留给你。

<?php
$cases = [
    [['start' => '09:00', 'end' => '01:00']],
    [['start' => '10:00', 'end' => '23:59']],
    [['start' => '10:00', 'end' => '00:00']],
    [['start' => '23:59', 'end' => '05:00']],
    [['start' => '10:00', 'end' => '15:00'], ['start' => '17:30', 'end' => '02:00']],
    [['start' => '10:00', 'end' => '15:00'], ['start' => '17:30', 'end' => '00:00']],
];

$cases = setUpCases($cases);

foreach ($cases as $case) {
    $withinTimeRange = isTimeWithinCase((new DateTime()), $case);
    if ($withinTimeRange) {
        break;
    }
}

// Boolean True/False whether it is within any of the ranges or not.
// i.e. True = Shop Open, False = Shop closed.
var_dump($withinTimeRange); 


function setUpCases($cases) {
    return array_map(function($case) {
        return convertToDateTimeObjects($case);
    }, $cases);
}

function convertToDateTimeObjects($case) {
    $formatted = [];
    foreach ($case as $index => $times) {
        $s = new DateTime();
        list($hour, $minute) = explode(':', $times['start']);
        $s->setTime($hour, $minute, 00);

        $e = new DateTime();
        list($hour, $minute) = explode(':', $times['end']);
        $e->setTime($hour, $minute, 00);

        if ($e < $s) $e->modify('+1 day');

        $formatted[] = ['start' => $s, 'end' => $e];
    }

    return $formatted;
}

function isTimeWithinCase($time, $case) {
    foreach ($case as $timerange) {
        return ($time > $timerange['start'] && $time < $timerange['end']);
    }
}

答案 3 :(得分:0)

我一直在仔细考虑这项任务。我想出了计算当前时间是否在2倍之间所需的逻辑。有两种情况可以计算:

案例1:开始时间大于结束时间

这意味着结束时间必须是第二天。这意味着如果您当前时间小于结束时间OR大于开始时间,则您的时间介于两次之间。

案例2:开始时间小于结束时间

在这种情况下,开始时间和结束时间是同一天。为此,需要的逻辑是检查您的时间是否大于开始时间AND小于结束时间。如果该陈述为真,则您的时间介于两次之间。

我已经计算了以下函数:

function checktime($arr,$time) {
    list($h,$m)=explode(":",$time);
    if(!is_array($arr[0])) {
        $r1=explode(":",$arr[0]);
        $r2=explode(":",$arr[1]);

        if($r1[0]>$r2[0]) {
            if(($h>$r1[0] || ($h==$r1[0] && $m>=$r1[1])) || ($h<$r2[0] || ($h==$r2[0] && $m<=$r2[1]))) return true;
        }
        else {
            if(($h>$r1[0] || ($h==$r1[0] && $m>=$r1[1])) && ($h<$r2[0] || ($h==$r2[0] && $m<=$r2[1]))) return true;
        }
    }
}

以下是一些测试,用于测试你给我的案例的限制:

$arr=array("10:00","01:00");
echo (checktime($arr,"23:59")?"True":"False");//True
echo (checktime($arr,"12:00")?"True":"False");//True
echo (checktime($arr,"00:00")?"True":"False");//True

$arr=array("10:00","23:59");
echo (checktime($arr,"23:59")?"True":"False");//True
echo (checktime($arr,"12:00")?"True":"False");//True
echo (checktime($arr,"00:00")?"True":"False");//False

$arr=array("10:00","00:00");
echo (checktime($arr,"23:59")?"True":"False");//True
echo (checktime($arr,"12:00")?"True":"False");//True
echo (checktime($arr,"00:00")?"True":"False");//True

$arr=array("23:59","05:00");
echo (checktime($arr,"23:59")?"True":"False");//True
echo (checktime($arr,"12:00")?"True":"False");//False
echo (checktime($arr,"00:00")?"True":"False");//True

$arr=array("08:35","15:30");
echo (checktime($arr,"23:59")?"True":"False");//False
echo (checktime($arr,"12:00")?"True":"False");//True
echo (checktime($arr,"00:00")?"True":"False");//False

$arr=array("17:30","02:00");
echo (checktime($arr,"23:59")?"True":"False");//True
echo (checktime($arr,"12:00")?"True":"False");//False
echo (checktime($arr,"00:00")?"True":"False");//True

$arr=array("10:00","15:00");
echo (checktime($arr,"23:59")?"True":"False");//False
echo (checktime($arr,"12:00")?"True":"False");//True
echo (checktime($arr,"00:00")?"True":"False");//False

$arr=array("17:00","00:00");
echo (checktime($arr,"23:59")?"True":"False");//True
echo (checktime($arr,"12:00")?"True":"False");//False
echo (checktime($arr,"00:00")?"True":"False");//True

答案 4 :(得分:0)

这适用于所有案例

       $let=array("01:00","02:00","03:00","05:00","04:00");                          
       if((strtotime($s)<=strtotime($myTime)) && (!strcmp($e,"00:00"))) 
                            { $open=1;$flag=1;}
       if((strtotime($s)<=strtotime($myTime)) && (!strcmp($e,"23:59"))) 
                            {  $open=1;$flag=1;}                               
      if(in_array($e,$let))
        {
         if(strtotime($s)<=strtotime($myTime))
            { $open=1;$flag=1;}
         else
         if(strtotime($e)>=strtotime($myTime))
            { $open=1;$flag=1;}                                 
        }
        if(!$flag)
            {if((strtotime($s)<=strtotime($myTime)) && (strtotime($e)>=strtotime($myTime)))
             { $open=1;}    }