gpt4 book ai didi

javascript - 在数据数组中查找下一个可用时隙

转载 作者:行者123 更新时间:2023-11-28 12:20:50 25 4
gpt4 key购买 nike

我有一个数组,其中包含一个“事件”数组,其中包含一个事件数组。

$data = array(
'events'=> array(
array(
'title' => 't1',
'start_time' => '12:00AM',
'end_time' => '1:00AM',
),
array(
'title' => 't1',
'start_time' => '1:00PM',
'end_time' => '1:30PM',
),
array(
'title' => 't1',
'start_time' => '1:30PM',
'end_time' => '2:00PM',
),
array(
'title' => 't1',
'start_time' => '3:15PM',
'end_time' => '3:45PM',
),
array(
'title' => 't1',
'start_time' => '3:45PM',
'end_time' => '4:45PM',
)
)
);

根据当前时间,我正在尝试查找下一个至少 15 分钟的可用时段。假设当前是下午 1:15,下一个可用时间段将是下午 2:00(基于提供的数据数组)。这看起来很简单,但我很困惑。

下面的代码是我所在的位置。如果当前时间发生事件,它将返回标题,但我不太确定如何获取下一个未被占用的时间段。

$time = time();
$timePlus15 = time() + 60*15;

foreach ($events->events as $key => $event) {
$start_time = strtotime($event->start_time);
$end_time = strtotime($event->end_time);

if (($time > $start_time && $timePlus15 < $end_time) || $time > $start_time && $time < $end_time || ($time < $start_time && $timePlus15 > $start_time && $timePlus15 < $end_time)) {
echo $event->title;
}
}

基本上,我想循环遍历数组。如果当前时间有事件,请将时间增加 15 分钟并再次检查。如果有事件,则增加 15 分钟并再次检查,重复直到该时间没有事件,然后返回该时间。

代码是 PHP 语言,但我也会接受 javascript/jquery 答案

** 我的问题的解决方案如下 **

this.setNextAvailableTime = function() {
var requiredGap = 15 * 60 * 1000;
var events = app.rooms.events[app.rooms.current_room].events;
var prev = events[0];
var firstGap = null;
if (events.length === 1) {
firstGap = prev.end_24time;
} else {
for (var i = 1; i < events.length; i += 1) {

var current = events[i];

var current_start = new Date();
var prev_end = new Date();
current_start.setHours(events[i].start_24time.substring(0,2),events[i].start_24time.substring(3,5), 0);
prev_end.setHours(prev.end_24time.substring(0,2),prev.end_24time.substring(3,5), 0);

var diff = current_start.getTime() - prev_end.getTime();

if( diff >= requiredGap) {
firstGap = prev.end_24time;
break;
}

prev = current;
}
}
//do something with the firstGap time
$('#start_time').val(firstGap);
};

最佳答案

可能更容易看出第一个事件的结束和下一个事件的开始之间的差异。这是一个 JavaScript 示例:

(请注意,字符串到日期的转换不是很可靠,这只是一些测试数据)

var events = [{
start: "2016-01-01T12:00",
end: "2016-01-01T13:00"
}, {
start: "2016-01-01T13:00",
end: "2016-01-01T13:30"
}, {
start: "2016-01-01T13:30",
end: "2016-01-01T14:00"
}, {
start: "2016-01-01T15:15",
end: "2016-01-01T15:45"
}, {
start: "2016-01-01T15:45",
end: "2016-01-01T16:45"
}];

var dateEvents = events.map(function(event) {
return {
start: new Date(event.start),
end: new Date(event.end)
};
});

var requiredGap = 15 * 60 * 1000;
var prev = dateEvents[0];
var firstGap = null;

for (var i = 1; i < dateEvents.length; i += 1) {
var current = dateEvents[i];
var diff = current.start - prev.end;

if (diff >= requiredGap) {
firstGap = {
start: prev.end,
end: current.start
};
break;
}

prev = current;
}

if (firstGap != null) {
console.log("First gap starts at: " + firstGap.start);
} else {
console.log("No gaps available");
}

关于javascript - 在数据数组中查找下一个可用时隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com