gpt4 book ai didi

php - 需要总时间中的空闲时间段

转载 作者:行者123 更新时间:2023-12-02 05:16:13 25 4
gpt4 key购买 nike

有一个音调,音调有自己的开始时间和结束时间(例如从 8:00 AM7:00 PM)。多场比赛已经安排在球场上一段时间了。 (例如,从 8:30AM9:00AM10:00AM10:30AM)所以现在我要计算的是球场上的可用时间。我的代码在这里:

$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'],
];

最佳答案

球场是免费的:

  1. 在比赛之间。
  2. opening time of the pitch 之间和 start of the first game .
  3. 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));

注意:

  1. 将日期时间作为字符串进行比较(可能)不是最佳做法。
  2. 上面的代码不是很优雅。
  3. 我还没有测试此代码以输出所需的结果。至少它应该是一个正确方向的好提示。

编辑:

在您的您的代码无法正常工作评论之后,我花了大约 10 秒的时间:

  1. 添加echo $g;到 for 循环。
  2. 注意 for 循环没有执行。
  3. 请注意,增量和条件被调换了。

从某种意义上说,我的代码是有效的——正如我所说的——它只是试图将您推向正确的方向。我强烈认为,花在快速修复上的时间不应该是我的,而是你的。不管怎样,长篇大论够了,我已经改变了:for ($g = 0; $g++; $g < count($plannedGames) - 1)for ($g = 0; $g < count($plannedGames) - 1; $g++) .希望对您有所帮助。

关于php - 需要总时间中的空闲时间段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51187628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com