Reputation: 103
I have this kind of scenario. Let say I have setting for overtime such as below. It can be in array.
Array From Until
[0] 5:00 AM 7:00 AM
[1] 6:00 PM 9:00 PM
Then, I will come to work to have an overtime. How to check if the actual overtime clocking is fall under the time range above?
For example, I come for overtime at:
OT Start: 6:00 AM , OT End: 9:00 AM
So, I will get
OT Start Allowed OT End Allowed
6:00 AM 7:00 AM
For more scenario, if
OT Start OT End OT Start Allowed OT End Allowed
4:00 AM 6:30 AM 5:00 AM 6:30 AM
5:30 AM 8:00 AM 5:30 AM 7:00 AM
5:30 PM 8:00 PM 6:00 PM 8:00 PM
6:30 PM 9:30 PM 6:30 PM 9:00 PM
5:30 PM 9:30 PM 6:00 PM 9:00 PM
5:30 AM 7:00 PM 5:30 AM 7:00 AM
6:00 PM 7:00 PM
6:00 AM 10:00 PM 6:00 AM 7:00 AM
6:00 PM 9:00 PM
That's how the system should handle the scenario. Anyone have idea how to do this in php? Thank you in advance.
Upvotes: 0
Views: 215
Reputation: 4218
For each pair of time ranges, ie one range of allowed hours and the actual hours, you need to check that the actual period starts before the allowed period ends and ends after it starts (or possibly on or after, if this is the case substitute >=
for >
). Then you just take the latest start time and the earliest end time.
$permitted_hours = [["05:00", "07:00"], ["18:00", "21:00"]];
$claims = [["04:00", "06:30"], ["05:30", "08:00"], ["17:30", "20:00"],
["18:30", "21:30"], ["17:30", "21:30"], ["05:30", "19:00"], ["06:00", "22:00"]];
echo "OT Start OT End OT Start Allowed OT End Allowed\n";
foreach ($claims as $claim) {
$first_match = true;
echo "$claim[0] $claim[1] ";
foreach ($permitted_hours as $permitted) {
if ($claim[0] < $permitted[1] && $claim[1] > $permitted[0]) {
if (!$first_match) {
echo "\n ";
}
$start = max($permitted[0], $claim[0]);
$end = min($permitted[1], $claim[1]);
echo "$start $end";
$first_match = false;
}
}
echo "\n";
}
/*
OT Start OT End OT Start Allowed OT End Allowed
04:00 06:30 05:00 06:30
05:30 08:00 05:30 07:00
17:30 20:00 18:00 20:00
18:30 21:30 18:30 21:00
17:30 21:30 18:00 21:00
05:30 19:00 05:30 07:00
18:00 19:00
06:00 22:00 06:00 07:00
18:00 21:00
*/
Upvotes: 2