有一个音高,并且音高有自己的开始时间和结束时间(例如,从SELECT COLUMN_ID, COLUMN_NAME, TABLE_NAME, DATA_TYPE, DATA_LENGTH, DATA_PRECISION, DATA_SCALE, NULLABLE, DATA_DEFAULT, IDENTITY_COLUMN
FROM user_tab_columns WHERE TABLE_NAME IN ('GPS') ORDER BY TABLE_NAME ASC, COLUMN_ID ASC;
到8:00 AM
)。并且已经预定了多场比赛并需要一段时间。 (例如,从7:00 PM
到8:30AM
,从9:00AM
到10:00 AM
)因此,现在我要计算的是可用的俯仰时间。我的代码在这里:
10:30 AM
答案 0 :(得分:-1)
俯仰是免费的:
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 ($g = 0; $g++; $g < count($plannedGames) - 1)
更改为for ($g = 0; $g < count($plannedGames) - 1; $g++)
。希望有帮助。