PHP数组逻辑-带有约会持续时间的时隙
我有一系列以15分钟为间隔的时段(小时:分钟,例如11:00、11.15) 因此,上面的数组表示11:00到12:00之间是空闲的 然后我有一个以分钟为单位的预约时间,例如60分钟或90分钟 我需要使用一些逻辑来根据预约时间确定是否有可用的时段。因此,如果预约持续时间为30分钟,我希望最终的数组为:PHP数组逻辑-带有约会持续时间的时隙,php,arrays,Php,Arrays,我有一系列以15分钟为间隔的时段(小时:分钟,例如11:00、11.15) 因此,上面的数组表示11:00到12:00之间是空闲的 然后我有一个以分钟为单位的预约时间,例如60分钟或90分钟 我需要使用一些逻辑来根据预约时间确定是否有可用的时段。因此,如果预约持续时间为30分钟,我希望最终的数组为: Array ( [0] => 11:00 [1] => 11:15 [2] => 11:30 [3] => 13:00 ) 11:45和13:15不包括在内,因为11:45
Array
(
[0] => 11:00
[1] => 11:15
[2] => 11:30
[3] => 13:00
)
11:45和13:15不包括在内,因为11:45到12:00和13:15到13:30仅为15分钟,因此不适合
如果预约持续时间为60分钟,我希望阵列
Array
(
[0] => 11:00
)
非常感谢您的帮助
谢谢我想我已经做到了,非常感谢您对改进代码的建议
$slotsNeeded = ceil($durationMinutes/15);
if($slotsNeeded <= count($workingSlotsReal)){
foreach($workingSlotsReal as $key => $value){
$notEnough = 0;
for($i = $key; $i < $key+$slotsNeeded; $i++){
if(!array_key_exists($i,$workingSlotsReal)){
$notEnough++;
}
}
if($notEnough > 0){
unset($workingSlotsReal[$key]);
}
}
} else {
unset($workingSlotsReal);
}
$slotseeded=ceil($durationMinutes/15);
如果($slotsNeeded$value){
$notEnough=0;
对于($i=$key;$i<$key+$slotsNeeded;$i++){
如果(!array_key_存在($i,$workingSlotsReal)){
$notEnough++;
}
}
如果($notEnough>0){
未设置($workingSlotsReal[$key]);
}
}
}否则{
未设置($workingSlotsReal);
}
我感觉有点慷慨,需要脑力锻炼,所以这里有一个可能的例子
(1) 您需要确定需要多少额外/连续插槽。由于您的时间间隔为15分钟增量,您需要将您的预约时间除以15分钟。如果允许非15分钟递增的约会,则需要使用将其四舍五入为整数
$blocks_needed = ceil($appointment_length/15);
如果您只需要额外的块量,您可以减去1
$additional_blocks_needed = ceil($appointment_length/15)-1;
(2) 您需要在插槽阵列中循环,并检查是否需要$blocks\u
连续插槽。或者简单地说,您可以检查当前时隙的$additional\u blocks\u needed
位置是否有数组值,以及距离当前时隙的$appointment\u length
分钟数
我把它作为一个函数来做,所以它是可重复的
function get_available_times($slots_array,$appointment_length){
// get 0-based additional blocks needed
$additional_blocks_needed = (ceil($appointment_length/15))-1;
// set an empty array
$available = array();
// loop through each $slot_array value
foreach($slots_array as $key=>$block){
// start with available as false
$slot_available = false;
// check if (1) there is a value at the `$key+($additional_blocks_needed)` slot
// AND
// check it the time from the last slot and the current slot is correct
if( isset($slots_array[$key+($additional_blocks_needed)]) &&
(strtotime($slots_array[$key+($additional_blocks_needed)])-strtotime($slots_array[$key]) == 900*($additional_blocks_needed)) )
{
// if both conditions are met, change to true
$slot_available=true;
}
// if true, then add to the available array
if($slot_available)
{
$available[]=$slots_array[$key];
}
}
// return the array of available slots
return $available;
}
您可以使用-
// your array of slots
$slots = array('11:00','11:15','11:30','11:45','13:00','13:15','13:30');
// loop through 10 min increments to see if there are available time slots
for($appointment=10;$appointment<=90;$appointment+=5){
$check = get_available_times($slots,$appointment);
echo "<pre>{$appointment}: ".print_r($check,1)."</pre>";
}
//您的插槽数组
$slots=array('11:00','11:15','11:30','11:45','13:00','13:15','13:30');
//循环10分钟的增量,查看是否有可用的时隙
对于($appointment=10;$appointment您试过写代码吗?您的问题几乎有道理,您只是缺少逻辑!我不知道该怎么做!我的想法是循环每个时间段,然后看看约会需要多少时间段,例如30分钟=2。我无法解决的问题是如何处理这个问题ey是4个连续的时段,就像我选择11:45一样,那么从技术上讲,之后有2个时段,但一个是11:45-12:00,另一个是13:00-13:15,这显然不好。预约时间也不能被15整除,因为它可能是80分钟等。是的,你需要循环每个时段,并检查是否有足够的连续时段。若要处理非15分钟增量,请在将时间除以15时将其四舍五入到下一个整数。我已提供了我的代码,请告知您是否可以使用更好的方法:-)如果这是您的解决方案,则可以作为答案发布。如果您需要有关改进代码的建议,请编辑您的问题,而不是作为答案发布。
// your array of slots
$slots = array('11:00','11:15','11:30','11:45','13:00','13:15','13:30');
// loop through 10 min increments to see if there are available time slots
for($appointment=10;$appointment<=90;$appointment+=5){
$check = get_available_times($slots,$appointment);
echo "<pre>{$appointment}: ".print_r($check,1)."</pre>";
}