Skip to content

Instantly share code, notes, and snippets.

@lkallas
Last active May 3, 2019 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkallas/1f25a49cd26c5ead2dbc0f6f3659215c to your computer and use it in GitHub Desktop.
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.
<?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
];
}
@lkallas
Copy link
Author

lkallas commented May 3, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment