1

I have 3 inputs for this :

1. $start_time = 21:00
2. $end_time = 09:00
3. $pickup_time = 23:00

What I want to check is, if the pickup_time is between start_time and end_time then return true otherwise return false.

I already tried this by converting all times using strtotime() and then checked them directly but that does not work here as that gives 21:00 as greater value then 09:00

And I do not want to check between 09:00 to 21:00 but I want to check between 21:00 to 09:00.

aslamdoctor
  • 3,753
  • 11
  • 53
  • 95
  • You would need some concept of date if 09:00 is *after* 21:00 – Adam Hopkinson Apr 08 '14 at 08:20
  • 2
    How can you know from which date are your times derived? I mean: `23:00` __is between__ `21:00` and `09:00` if `09:00` is for _next date_, but __not between__ them if they are from same date – Alma Do Apr 08 '14 at 08:20
  • Let's say I have only a start Date as 10-12-2014 ? – aslamdoctor Apr 08 '14 at 08:21
  • possible duplicate of [How to get time difference in minutes in PHP](http://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php) – jancha Apr 08 '14 at 08:21
  • @AlmaDo mentioned time comaprison without the actual date will not be correct. `21:00` as startime means it 9PM and endtime `09:00` means 9AM so this implies next day or same day morning ? It does not tell anything and hence comparison becomes complicated. If a date is attached with the time then it becomes easy.. – Abhik Chakraborty Apr 08 '14 at 08:29

3 Answers3

1

I think the logic should be like this:

if($end_time > $start_time)
    # 09:00 .. 21:00
    $valid = $pickup_time > $start_time && $pickup_time < $end_time;
else
    # 21:00 .. 09:00
    $valid = $pickup_time > $start_time || $pickup_time < $end_time;
gog
  • 10,367
  • 2
  • 24
  • 38
1

I hope the below code will solve your problem, if you have any question please let me know

function is_between_pickup_time($start_time, $end_time, $pickup_time) {

    // if start time greater than end time
    if (strtotime($start_time) >= strtotime($end_time)) {
        return false;
    }

    // if pickup time is between
    if (strtotime($pickup_time) > strtotime($start_time) && strtotime($pickup_time) < strtotime($end_time)) {
        return true;
    }

    // defaul return false;
    return false;
}

// start, end and pickup time
$start_time = '04-08-14 08:32:37';
$end_time = '04-08-14 10:32:37';
$pickup_time = '04-08-14 09:32:3';

is_between_pickup_time($start_time, $end_time, $pickup_time);
solvease
  • 529
  • 2
  • 11
0

strtotime() makes you an unixtimestamp. When you don't give a date the 21:00 will be later then 9:00, so you could add a date so the unixtimestamp of 9:00 will be higher then 21:00 of the previous day.

You could also fix this is ugly way to check if the value is between start and 23:59 or 0:00 and 9:00 but then those values have to be static which I believe they are not :)

Jesper
  • 599
  • 4
  • 9