0

i need to check whether the current time lies between the range

these r restaurant open timings ,timings can be like open now till next morning or closed for sometime same day and open next half same day till next day or midnight      
 examples

        Case1: 10:00  01:00
        Case2: 10:00  23:59
        Case3: 10:00  00:00
        Case4: 23:59  05:00
        Case5: 08:35  15:30 && 17:30 02:00
       Case6: 10:00  15:00 && 17:00 00:00

all the cases must be checked

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

this is not working where m i going wrong

Tested
  • 761
  • 4
  • 16
  • 38
  • In the first case, is second row the following day or the same day just 9 hours before? – 131 Aug 11 '17 at 08:12
  • I recommend to use DateTime objects. Store your 'case' in DateTime objects and compare them with current date. See https://stackoverflow.com/questions/961074/how-do-i-compare-two-datetime-objects-in-php-5-2-8 for examples. – user1915746 Aug 11 '17 at 08:18
  • @131 different days different examples on 5/6 can be whole day time – Tested Aug 11 '17 at 08:20
  • @user1915746 the thing is i have to compare with current time the range – Tested Aug 11 '17 at 08:23
  • @hir: I posted an example as answer. – user1915746 Aug 11 '17 at 08:43
  • Do you only have the time to go off, or do you have the other date information associated with it, i.e. day. – Mikey Aug 11 '17 at 08:47
  • @Pigeon these r restaurant open timings ,timings can be like open now till next morning or closed for sometime same day and open next half same day till next day or midnight – Tested Aug 11 '17 at 08:49

5 Answers5

1

Something like this might work. I've not really tested it.

$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.
David Goodwin
  • 4,184
  • 1
  • 22
  • 12
  • When the second time is less than the first, are you expecting it to be in the next day? For example - with "10:00 to 00:00" - is that between 10:00 am this morning and midnight tonight - or should it be midnight last night? – David Goodwin Aug 11 '17 at 08:44
  • this morning and midnight tonight -- these r restuarant open timings – Tested Aug 11 '17 at 08:46
1

Use DateTime objects for comparison like this. See 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;
    }
}
user1915746
  • 466
  • 4
  • 16
  • @hir: see my updated answer. You can adjust the dates in the cases array. Your cases 5 and six are just a combination of two single cases. – user1915746 Aug 11 '17 at 09:02
0

Right-o, had a mess around for you. Do you want to see if this is getting your expected output? Seems to be working on my local tests. Fair bit of code mind, turned out to be a few edge cases to consider.

Potentially bits of it that can be refactored and improved, but i'll leave that up to you if it works as intended.

<?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']);
    }
}
Mikey
  • 2,606
  • 1
  • 12
  • 20
0

I have been thinking about this task carefully. And i came up with the logic that is required to calculate whether the current time is in between 2 times. There are the following two cases to calculate that:

Case 1: The starting time is greater than the ending time

Which means the ending time must be the next day. That means that if your current time is less than the ending time OR greater than the start time, your time is in between the two times.

Case 2: The starting time is less than the ending time

In this case, the start time and the ending time is the same day. For this, the logic that is needed is to check whether or not your time is greater than the start time AND less than the ending time. If that's statement is true, your time is in between the two times.

I have made the following function for calculating that:

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

And here are some tests, for testing the limits of the cases you gave me:

$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
Community
  • 1
  • 1
131
  • 1,363
  • 1
  • 12
  • 32
0

this works for all the casese

       $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;}    }
Tested
  • 761
  • 4
  • 16
  • 38