需要总时间中的空闲时间

时间:2018-07-05 09:18:34

标签: php laravel date datetime php-carbon

有一个音高,并且音高有自己的开始时间和结束时间(例如,从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 PM8:30AM,从9:00AM10:00 AM)因此,现在我要计算的是可用的俯仰时间。我的代码在这里:

10:30 AM

1 个答案:

答案 0 :(得分:-1)

俯仰是免费的:

  1. 在比赛之间。
  2. opening time of the pitchstart of the first game之间。
  3. end of the last gameclosing 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++)。希望有帮助。