Php 需要总时间中的空闲时间段
有一个音高,音高有自己的开始时间和结束时间(例如从Php 需要总时间中的空闲时间段,php,laravel,date,datetime,php-carbon,Php,Laravel,Date,Datetime,Php Carbon,有一个音高,音高有自己的开始时间和结束时间(例如从8:00am到7:00pm)。多场比赛已经在球场上安排了一段时间。(例如从8:30AM到9:00AM,10:00AM到10:30AM),所以现在我要计算的是球场上的可用时间。我的代码在这里: $pitchStart = '2018-06-11 08:00 AM'; $pitchClose = '2018-06-11 09:00 PM'; $firstGameStart = '2018-06-11 09:30 AM'; $firstGameEnd
8:00am
到7:00pm
)。多场比赛已经在球场上安排了一段时间。(例如从8:30AM
到9:00AM
,10:00AM
到10: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'],
];
球场是免费的:
球场开始时间
和第一场比赛开始之间
结束
和球场结束时间之间
沿着这些思路的一些东西可能会起作用:
<?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));
<代码>我认为中间的代码位不见了。你能补充一下到目前为止你已经尝试过的吗?实际上我只需要逻辑(方法)来实现上面的一个。很抱歉,我不能分享代码,因为它包含了太多的东西,而且代码也很长@Kikosoftware最基本的算法是:1。把你所有的时间都分类。2.第一次告诉你球场什么时候开始,所以这是一个空位的开始。3.下一次告诉你第一场比赛的开始,所以这是一个免费的插槽结束。4.继续消耗空闲插槽的开始和完成时间,直到结束。最后:这是基于没有计划重叠游戏的假设。嗨@Nielles,我尝试过上面的代码,但它只返回-pitchStart和第一场比赛开始之间的时间-pitchEnd和最后一场比赛结束之间的时间,但不返回游戏之间的时间。Thanks@SunnySheth代表未来的读者;谢谢你接受答案!嗨@Nielles,如果你能帮忙的话,我还面临一个问题。在这里,我们预计只有一个球场开始时间和结束时间<代码>$pitchOpeningTimes=['pitchStart'=>'2018-06-11 08:00 AM','pitchClose'=>'2018-06-11 09:00 PM']代码>但在我的例子中,有多个音高开始时间和结束时间。例如$pitchOpeningTimes=['pitchStart'=>'2018-06-11 08:00 AM',['pitchClose'=>'2018-06-11 09:00 PM',['pitchStart'=>'2018-06-11 12:00 AM','pitchClose'=>'2018-06-11 03:00 PM',]@SunnySheth此添加内容超出了您原来问题的范围,应该是一个新问题。也就是说,在你自己做了一些尝试之后,你还是无法找到答案。方向如下:
foreach($pitchOpeningTimes as$pitch=>$times){$openSlots[$pitch]=openingTimes($times,$games);}
可以工作。虽然这不会像现在这样起作用,因为$games
数组中没有区分在哪个球场上玩哪个游戏。