[英]Merging time periods - php
I have the info (start timestamp and finish timestamp) on the each red marked period. 我在每个红色标记的期间都有信息(开始时间戳和结束时间戳)。
what I want to achieve is this: 我要实现的是:
The final period should contain also number of merged periods as shown in blue span. 最终期间也应包含合并期间的数量,如蓝色跨度所示。
I've tried iterating each minute since min and max values but I guess that's not optimal way to solve this. 自最小值和最大值以来,我尝试过每分钟进行迭代,但是我想这不是解决此问题的最佳方法。 I'm actually running out of ideas here : ( Example data:
我实际上在这里用尽了想法:(示例数据:
[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
)
Answer thanks to @solarc 感谢@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'],
];
}
}
}
Make a set of all the start and end times (without duplicates) 设定所有开始和结束时间(无重复)
[01:30, 05:30, 09:00, 10:00, 14:00, 18:30]
Split each block if there's a time from the set between its own start and end times (so for example the block from 09:00 to 15:00 is split into 2: 09:00 to 10:00 and 10:00 to 15:00): 如果每个块在其自己的开始时间和结束时间之间存在时间,则将其拆分(例如,从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)
Finally, count how many blocks are repeated across columns: 最后,计算跨列重复多少个块:
- 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.