Checking if a time range contains another time range in PHP

Hi Can you help me in my problem?

checking if a time range contains another time range for example I have 2 time range:

$nightShiftStart = strtotime("22:00:00 today");
$nightShiftEnd = strtotime("06:00:00 tomorrow");
$overTimeStart = strtotime("21:00:00 today");
$overTimeEnd = strtotime("07:00:00 tomorrow");

if I check if overtime start and end contains night shift schedule it should return true. And I have a code for that.

if ($overTimeStart >= $nightShiftStart && $overTimeEnd <= $nightShiftEnd ) {
    return true;
} else {
    return false;

the code above will return true. but if I change the overtime range to this:

$nightShiftStart = strtotime("22:00:00 today");
$nightShiftEnd = strtotime("06:00:00 tomorrow");
$overTimeStart = strtotime("17:00:00 today");
$overTimeEnd = strtotime("20:00:00 today");

it also return true, it should return false because the overtime is only from 5pm to 8pm and the time not meet the Night Shift. Can you pls help me with this i’ve been stuck for 2days.

>Solution :

Take a look at the document:

Exceptions to this rule are: "yesterday", "midnight", "today", "noon" and "tomorrow". Note that "tomorrow 11:00" and "11:00 tomorrow" are different. Considering today’s date of "July 23rd, 2008" the first one produces "2008-07-24 11:00" where as the second one produces "2008-07-24 00:00". The reason for this is that those five statements directly influence the current time.

So the correct strings should be:

$nightShiftStart = strtotime("today 22:00:00");
$nightShiftEnd = strtotime("tomorrow 06:00:00");
$overTimeStart = strtotime("today 17:00:00");
$overTimeEnd = strtotime("today 20:00:00");

Leave a Reply