There is one pitch and pitch has its own start time and end time (e.g from 8:00 AM
to 7:00 PM
). And multiple matches has already been scheduled on pitch with some time. (e.g from 8:30AM
to 9:00AM
, 10:00 AM
to 10:30 AM
) So now what i want to calculate is available time on pitch. My code is here:
$pitchStart = '2018-06-11 08:00 AM';
$pitchClose = '2018-06-11 09:00 PM';
$firstGameStart = '2018-06-11 09:30 AM';
$firstGameEnd = '2018-06-11 10:00 AM';
$secondGameStart = '2018-06-11 10:00 AM';
$secondGameEnd = '2018-06-11 10:30 AM';
$thirdGameStart = '2018-06-11 11:00 AM';
$thirdGameEnd = '2018-06-11 11:30 AM';
$Result = [
[0] => ['freeSlotStart' => '2018-06-11 08:00 AM','freeSlotEnd' => '2018-06-11 09:30 AM'],
[1] => ['freeSlotStart' => '2018-06-11 10:30 AM','freeSlotEnd' => '2018-06-11 11:00 AM'],
[2] => ['freeSlotStart' => '2018-06-11 11:30 AM','freeSlotEnd' => '2018-06-11 09:00 PM'],
];
The pitch is free:
opening time of the pitch
and start of the first game
.end of the last game
and closing time of the pitch
.If you'd store the games in array instead of variables, you can simply determine the times for the above cases -- if applicable -- by looping over the array.
Something along these lines may work:
<?php
$pitchOpeningTimes =
[
'pitchStart' => '2018-06-11 08:00 AM',
'pitchClose' => '2018-06-11 09:00 PM'
];
$games = [
[
'GameStart' => '2018-06-11 09:30 AM',
'GameEnd' => '2018-06-11 10:00 AM',
],
[
'GameStart' => '2018-06-11 10:00 AM',
'GameEnd' => '2018-06-11 10:30 AM',
],
[
'GameStart' => '2018-06-11 11:00 AM',
'GameEnd' => '2018-06-11 11:30 AM',
]
]; // Assuming these are sorted ascending, if this assumption is wrong, sort it.
function openSlots($openingTimes, $plannedGames)
{
if (count($plannedGames) == 0) { # No games planned, pitch is free all day.
return ['freeSlotStart' => $openingTimes['pitchStart'], 'freeSlotEnd' => $openingTimes['pitchClose']];
}
$freeslots = []; # We need a result array to push our free slots to.
// First edge case: pitch might be free between pitchStart and start of the first game
// if game doesn't start at opening of the pitch.
if ($plannedGames[0]['GameStart'] !== $openingTimes['pitchStart']) {
$freeslots[] = [
'freeSlotStart' => $openingTimes['pitchStart'],
'freeSlotEnd' => $plannedGames[0]['GameStart']
];
}
// Loop over the games to check for open slots between games.
for ($g = 0; $g < count($plannedGames) - 1; $g++) {
if ($plannedGames[$g]['GameEnd'] !== $plannedGames[$g + 1]['GameStart']) {
// echo $g;
$freeslots[] = [
'freeSlotStart' => $plannedGames[$g]['GameEnd'],
'freeSlotEnd' => $plannedGames[$g + 1]['GameStart']
];
}
}
// Second edge case: pitch might be free between pitchEnd and end of the last game
// If game doesn't end at the time the pitch closes.
$lastGame = end($plannedGames);
if ($lastGame['GameEnd'] !== $openingTimes['pitchClose']) {
$freeslots[] = [
'freeSlotStart' => $lastGame['GameEnd'],
'freeSlotEnd' => $openingTimes['pitchClose']
];
}
return $freeslots;
}
var_dump(openSlots($pitchOpeningTimes, $games));
Note:
After your your code is not working comment, I took about 10 seconds of my time to:
echo $g;
to the for loop.My code was working, in the sense -- as I stated -- that it was merely an attempt to push you in the right direction. I strongly feel that it shouldn't have been my time, but yours, that was spent on this quick fix. Anyway, enough with the tirade, I've changed: for ($g = 0; $g++; $g < count($plannedGames) - 1)
to for ($g = 0; $g < count($plannedGames) - 1; $g++)
. Hope that helps.