0

I have this time range in array example:

$timerange = array('01:30:00','01:31:00',...........,'02:30:00');

and 2 variable:

$start_time = '01:15:00';
$end_time = '03:29:00';

if($timerange is between $start_time && $end_time)
{
//do it something if yes.....
}

Please help me, its have any ready function to use in PHP? to check on this.

Shah Rezza
  • 27
  • 8
  • does start AND end time need to be within, or can it overlap? – delboy1978uk Oct 02 '18 at 09:09
  • @delboy1978uk it can be overlap also – Shah Rezza Oct 02 '18 at 09:14
  • ok so start time can be before, so long as the end time is within, or, end time can be afterwards, so long as start time is within? – delboy1978uk Oct 02 '18 at 09:15
  • My bad, the actual dupe is: https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php - The above linked dupe is also a dupe pointing to the question that I've linked in this comment. – Script47 Oct 02 '18 at 09:15

3 Answers3

0

See How to check if a date is in a given range? Edit: As you are looking in a 24 hour range you can pick and random date when constructing your timestamps and your calculations should hold true as all of them are the same date.

return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));

Where all of those are timestamps.

Also look at this PHP check if time falls within range, questioning common solution if you don't want this to depend on the date but just the time.

  • The distinct difference being that in this question and your first linked question is `strtotime` which returns a UNIX timestamp from a date. How would you do that based on the OP's variables when they are just time? – Script47 Oct 02 '18 at 09:14
  • 1
    this solution really not helping on my cased because the time range is in array format and need to compare with 2 variable. – Shah Rezza Oct 02 '18 at 09:18
  • 2
    Right I see why the down vote, however you can achieve the same effect by just picking any date and comparing, if we only look at those in a 24h window anyway. I'll edit my answer. Re: the times being in an array, do you want to check if all the strings are in the limits and then do one thing if all are and another if some of them are not? – Eleonora Ivanova Oct 02 '18 at 09:34
0
<?php 
$timerange = array(strtotime('01:30:00'), strtotime('01:31:00'), strtotime('03:30:00'));
$start_time = strtotime('01:15:00');
$end_time = strtotime('03:29:00');

foreach($timerange as $key => $text_field){
    if($timerange[$key] > $start_time && $timerange[$key] < $end_time){
        echo "Existing";
    }else{
        echo "Not Existing";
    }
}
?>
0

You need not bother with conversions of your time strings to a time type - you can compare the strings as they are:

<?php
$timerange = array('01:30:00', '01:31:00', '01:32:00', '02:30:00');
$start_time = '01:15:00';
$end_time = '03:29:00';

$between = array();
foreach ($timerange as $time)
    if ($start_time <= $time && $time <= $end_time) $between[] = $time;
if ($between)
{
    echo "These are the times between $start_time and $end_time:\n";
    print_r($between);
}

If you like it better, you can replace the foreach loop with array_filter():

$between = array_filter($timerange,
                        function($time) use ($start_time, $end_time)
                        { return $start_time <= $time && $time <= $end_time; }
                       );
Armali
  • 18,255
  • 14
  • 57
  • 171