输入多个时间范围然后返回所有另一个时间范围

我正在寻找一个解决方案,当我输入一个时间范围(或多个时间范围)作为数组时,它将返回除我输入的时间范围之外的所有其他时间范围。

例如:
我输入[ 0 => ['start' => '8:30:00', 'end' => '9:00:00'], 1 => ['start' => '11:30:00', 'end' => '12:30:00']]

(8:30:00 - 9:00:00), (11:30:00 - 12:30:00)

我一直想从8:00:00到收到17:00:00

我希望它返回的预期输出是:
[ 0 => ['start' => '8:00:00', 'end' => '8:30:00'], 1 => ['start' => '09:00:00', 'end' => '11:30:00'], 2 => ['start' => '12:30:00', 'end' => '17:00:00']]

(8:00:00 - 8:30:00), (09:00:00 - 11:30:00), (12:30:00 - 17:00:00)

谢谢你。


aluckdog
浏览 244回答 1
1回答

慕田峪4524236

您可以简单地使用这个概念。您不需要使用任何日期时间库或任何其他概念。只需使用 for 循环即可正常工作。<?php$startingTime = '8:00:00';$endingTime = '1:00:00';$time = [ ['start' => '8:30:00', 'end' => '9:00:00'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;['start' => '11:30:00', 'end' => '12:30:00']];$output = [];for ($x = 0; $x <= count($time); $x++) {&nbsp; &nbsp; $t = $time[$x];&nbsp; &nbsp; $interval = [];&nbsp; &nbsp; $interval['start'] = $startingTime;&nbsp; &nbsp; $interval['end'] = ($t['start']=== NULL? $endingTime: $t['start'] );&nbsp; &nbsp; array_push($output,$interval);&nbsp; &nbsp; $startingTime = $t['end'];}print_r($output);
打开App,查看更多内容
随时随地看视频慕课网APP