找到空闲时间间隔

时间:2018-01-08 17:36:22

标签: php

有一个包含空闲时间间隔的数组和一些其他信息:

[2018-01-10] => Array
    (
        [11:00-12:00] => Array
            (
                [direction] => 'Test1'
                [color] => 'danger'
                [group] => 'group 1'
            )

        [18:00-19:00] => Array
            (
                [direction] => 'Test2'
                [color] => 'danger'
                [group] => 'group 2'
            )

        [19:00-21:30] => Array
            (
                [direction] => 'Test2'
                [color] => 'danger'
                [group] => 'group 1'
            )

    )

我尝试获得一个新的数组,其中包含空闲时间间隔,也没有空闲时间间隔:

[2018-01-10] => Array
    (
        [00:00-11:00] => Array
            (
                [direction] => null
                [color] => 'success'
                [group] => null
            )

        [11:00-12:00] => Array
            (
                [direction] => 'Test1'
                [color] => 'danger'
                [group] => 'group 1'
            )

        [12:00-18:00] => Array
            (
                [direction] => null
                [color] => 'success'
                [group] => null
            )

        [18:00-19:00] => Array
            (
                [direction] => 'Test2'
                [color] => 'danger'
                [group] => 'group 2'
            )

        [19:00-21:30] => Array
            (
                [direction] => 'Test2'
                [color] => 'danger'
                [group] => 'group 1'
            )

        [21:30-23:59] => Array
            (
                [direction] => null
                [color] => 'success'
                [group] => null
            )
    )

如果没有忙碌时间,请回答[00:00-23:59]

我该怎么办?任何变种?我应该对数组进行排序,减去,使用很多条件吗?或者是什么?

$start_time=strtotime('00:00');
$end_time=strtotime('23:59');
    foreach ($occupancy as $date => $data) {
        foreach ($data as  $time => $value) {
            $current_interval=explode('-', $time);
            $current_start=strtotime($current_interval[0]);
            $current_end=strtotime($current_interval[1]);

            //???
        }
    }

1 个答案:

答案 0 :(得分:0)

对于数组中的每个键(日期),您将获得一个包含多个键的数组,格式为[11:00-12:00]。

所以你要做的就是将该数组传递给构建"空闲时间数组的函数":

function fillTimes(array $times) {
    ksort($times);
    $expect= '00:00';
    $filled= [ ];
    $empty = [
        'direction' => null,
        'color'     => 'success',
        'group'     => null
    ];
    foreach ($times as $hhmm => $info) {
        list ($start, $stop) = explode('-', $hhmm);
        if ($start !== $expect) {
             // We were expecting say 00:00 but we found 02:00
             $free = "{$expect}-{$start}";
             $filled[$free] = $empty;
        }
        $filled[$hhmm] = $info;
        // Next period begins where the last stops - to 12:00, from 12:00
        // If it was from 12:00, but to 11:59, this would be a bit harder
        $expect = $stop;
    }
    if ($expect !== '23:59') {
        $filled["{$expect}-23:59"] = $empty;
    }
    return $filled;
}

请注意,上述代码要求所有小时数除了最后一个以小时结束 - 所以11:00-12:00,而不是11:00-11:59。如果您想使用第二种形式,则需要更仔细地解析小时和分钟。