Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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: https://www.php.net/manual/en/datetime.formats.relative.php

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");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading