合并时间段-PHP

时间:2018-11-07 16:56:15

标签: php

我在每个红色标记的时间段上都有信息(开始时间戳和结束时间戳)。

erg

我想要实现的是: enter image description here

最后一个期间也应包含合并期间的数量,如蓝色跨度所示。

我已经尝试过从最小值和最大值开始的每一分钟,但是我想这不是解决此问题的最佳方法。 我实际上在这里没有想法了:( 示例数据:

[0] => Array
    (
        [start] => 2018-11-8 09:00
        [startTimestamp] => 1541664000
        [finish] => 2018-11-8 15:00
        [finishTimestamp] => 1541685600
        [machine_id] => 1
    )

[1] => Array
    (
        [start] => 2018-11-8 01:30
        [startTimestamp] => 1541637000
        [finish] => 2018-11-8 05:30
        [finishTimestamp] => 1541651400
        [machine_id] => 1
    )

[2] => Array
    (
        [start] => 2018-11-8 10:00
        [startTimestamp] => 1541667600
        [finish] => 2018-11-8 18:30
        [finishTimestamp] => 1541698200
        [machine_id] => 4
    )

[3] => Array
    (
        [start] => 2018-11-8 09:00
        [startTimestamp] => 1541664000
        [finish] => 2018-11-8 15:00
        [finishTimestamp] => 1541685600
        [machine_id] => 5
    )

[4] => Array
    (
        [start] => 2018-11-8 01:30
        [startTimestamp] => 1541637000
        [finish] => 2018-11-8 05:30
        [finishTimestamp] => 1541651400
        [machine_id] => 5
    )

感谢@solarc

        $startEndTimes = [];
    foreach ($periodsData as $periodsDatum) {
        $startEndTimes[$periodsDatum['startTimestamp']]  = date('Y-m-d H:i:s', $periodsDatum['startTimestamp']);
        $startEndTimes[$periodsDatum['finishTimestamp']] = date('Y-m-d H:i:s', $periodsDatum['finishTimestamp']);
    }

    $split = [];
    foreach ($periodsData as $periodsDatum) {
        foreach ($startEndTimes as $timestamp => $dateTime) {
            if ($timestamp < $periodsDatum['finishTimestamp'] AND $timestamp > $periodsDatum['startTimestamp']) {
                $split[] = [
                    'machine_id'      => $periodsDatum['machine_id'],
                    'startTimestamp'  => $periodsDatum['startTimestamp'],
                    'finishTimestamp' => $timestamp,
                    'start'           => $periodsDatum['start'],
                    'finish'          => date('Y-n-j H:i:s', $timestamp),
                ];
                $split[] = [
                    'machine_id'      => $periodsDatum['machine_id'],
                    'startTimestamp'  => $timestamp,
                    'finishTimestamp' => $periodsDatum['finishTimestamp'],
                    'start'           => date('Y-n-j H:i:s', $timestamp),
                    'finish'          => $periodsDatum['finish'],
                ];
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

  1. 设置一组所有开始和结束时间(无重复)

    [01:30, 05:30, 09:00, 10:00, 14:00, 18:30]
    
  2. 如果每个块在其自己的开始时间和结束时间之间有时间间隔,则将其拆分(例如,从09:00到15:00的块被拆分为2:09:00到10:00,然后10:00至15:00):

        - 01:30 to 05:30 (col 1)
        - 09:00 to 10:00 (col 1)
        - 10:00 to 15:00 (col 1)
        - 10:00 to 15:00 (col 2)
        - 15:00 to 18:30 (col 2)
        - 01:30 to 05:30 (col 3)
        - 09:00 to 10:00 (col 3)
        - 10:00 to 15:00 (col 3)
        - 10:00 to 15:00 (col 4)
        - 15:00 to 18:30 (col 4)
    
  3. 最后,计算跨列重复的块数:

        - 01:30 to 05:30 (2 times)
        - 09:00 to 10:00 (2 times)
        - 10:00 to 15:00 (4 times)
        - 15:00 to 18:30 (2 times)