php - 需要总时间的空闲时间段
问题描述
有一个音高,音高有自己的开始时间和结束时间(例如从8:00 AM
到7:00 PM
)。并且已经在一段时间内安排了多场比赛。(例如从8:30AM
到9:00AM
,10:00 AM
到10:30 AM
)所以现在我要计算的是在球场上的可用时间。我的代码在这里:
$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'],
];
解决方案
球场是免费的:
- 在游戏之间。
- 之间
opening time of the pitch
和start of the first game
。 - 之间
end of the last game
和closing time of the pitch
。
如果您将游戏存储在数组中而不是变量中,您可以通过遍历数组来简单地确定上述情况的时间(如果适用)。
这些方面的东西可能会起作用:
<?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));
笔记:
- 将日期时间与字符串进行比较(可能)不是最佳实践。
- 上面的代码不是很优雅。
- 我尚未测试此代码以输出所需的结果。至少它应该是正确方向的一个很好的提示。
编辑:
在您的代码无效评论后,我花了大约 10 秒的时间来:
- 添加
echo $g;
到 for 循环。 - 请注意,for 循环没有执行。
- 请注意增量和条件交换的事实。
我的代码是有效的,从某种意义上说——正如我所说——它只是试图将你推向正确的方向。我强烈认为不应该是我的时间,而是你的时间,花在了这个快速修复上。无论如何,长篇大论已经足够了,我已经改变:for ($g = 0; $g++; $g < count($plannedGames) - 1)
到for ($g = 0; $g < count($plannedGames) - 1; $g++)
. 希望有帮助。