Last active
May 3, 2019 15:41
-
-
Save lkallas/1f25a49cd26c5ead2dbc0f6f3659215c to your computer and use it in GitHub Desktop.
Solution for Astro Baltics on-site test-assignment that must be solved in 1-2 hours.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function calculateWorkingHours(string $workStartTime, string $workEndTime, string $dayStartTime = '06:00', string $dayEndTime = '22:00'): array | |
{ | |
$dateTime = DateTime::createFromFormat('H:i', $workStartTime); | |
$timeCursor = $dateTime->format('H:i'); | |
$interval = new DateInterval('PT15M'); | |
$dayUnits = $nightUnits = 0; | |
do { | |
$nextInterval = $dateTime->add($interval); | |
$timeCursor = $nextInterval->format('H:i'); | |
if ($dayStartTime < $timeCursor && $dayEndTime >= $timeCursor) { | |
$dayUnits++; | |
} else { | |
$nightUnits++; | |
} | |
} while ($workEndTime != $timeCursor); | |
return [ | |
'day' => $dayUnits / 4, | |
'night' => $nightUnits / 4 | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Write a function that calculates night shift (22:00 - 06:00) hours and dayshift hours (06:00 - 22:00) between given range of time. Function takes 2 parameters that represent work starting time and end time. Input parameters are 15-minutes precision (quarter of an hour) and do not need to be validated, you can safely assume that they are correct. Your function must respect the quarter of an hour precision in calculations as well.
For example function("20:15", "03:00") would return 1.75h day shift and 5h night shift.