PHP中的Time Range Array问题

2lpgd968  于 2023-06-20  发布在  PHP
关注(0)|答案(4)|浏览(142)

我试图写一个应用程序预订一个时间槽在机场,基本上有一个开放和关闭的时期和时间间隔,然后应用程序之间的两个时间值循环,并显示一个选择下拉菜单。
我需要为机场建立一个选项来阻止时间范围,例如每个星期四上午10点到下午12点之间机场关闭预订,我需要将此构建到循环中,但无法使其工作。它需要是特定的一天(周一至周日),而不是特定的日期,因为机场将在同一时间段内每周的每个指定日期关闭。
这是我的代码,有谁可以帮忙吗?

<?php

$open_from = "09:00";
$open_to = "17:00";
$interval = "15 mins";  

$today_number = date('w');

$intervals = array(
    array(
        'day' => '4',
        'start' => '10:00',
        'end' => '12:00',
    ),
    array(
        'day' => '4',
        'start' => '12:30',
        'end' => '13:30',
    ),
    array(
        'day' => '5',
        'start' => '15:30',
        'end' => '16:00',
    ),
);  

if(!isset($_GET['date'])){                  
    $date = date('d-m-Y');          
}else{          
    $date = $_GET['date'];          
} 

function create_time_range($start, $end, $interval) { 

    $start_time = strtotime($start); 
    $end_time   = strtotime($end); 

    $current    = time(); 
    $add_time   = strtotime('+'.$interval, $current); 
    $diff       = $add_time-$current; 

    $times = array(); 
    while ($start_time < $end_time) { 
        $times[] = $start_time; 
        $start_time += $diff; 
    } 
    $times[] = $start_time; 
    return $times; 
    
}

$times = create_time_range($open_from, $open_to, $interval);

foreach ($times as $key => $time) { 
    $times[$key] = date($date.'H:i', $time); 

    foreach($intervals as $key2 => $item) {
        if($intervals[$key2]['day']==$today_number && date('H:i',$time) == $intervals[$key2]['start'] && date('H:i',$time) == $intervals[$key2]['end']){
            echo "yes";
        }else{
            echo "no";
        }
    echo "<option value='".date('H:i', $timestamp)."'>".date('H:i',$time)."</option><br>";      
    //echo $intervals[$key]['day']." - ".$intervals[$key]['start']." - ".$intervals[$key]['end'];
    //echo "<br>";
}

    
    
}
?>
nr7wwzry

nr7wwzry1#

试试这个方法:

$interval = "15 minutes";
...
$current    = time();
add_time   = strtotime($current.'+'.$interval);

你也可以给我们更多的信息,最终错误抛出。最终启用调试:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

更新1
在foreach中,你试图修改你正在循环的同一个数组($times [$key]),似乎没有意义。

axr492tv

axr492tv2#

看看这个我希望我已经正确理解了您的问题并提供了所需的解决方案。

<?php
$startTime     = '09:00';
$endTime       = '17:00';
$interval      = 15; // minutes
$weekNumber    = date('w');
$excludedSlots = [
    ['day' => '4', 'start' => '10:00', 'end' => '12:00'],
    ['day' => '4', 'start' => '12:30', 'end' => '13:30'],
    ['day' => '5', 'start' => '15:30', 'end' => '16:00']
];

$timeRange = generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots);
echo $timeRange;

function generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots) {
    $timeRange = '';
    $currentTime = $startTime;

    while ($currentTime <= $endTime) {
        $isExcluded = false;

        foreach ($excludedSlots as $excludedSlot) {
            if ($weekNumber == $excludedSlot['day'] && $currentTime >= $excludedSlot['start'] && $currentTime < $excludedSlot['end']) {
                $isExcluded = true;
                break;
            }
        }

        if (!$isExcluded) {
            $timeRange .= "<option value='{$currentTime}'>{$currentTime}</option>";
        } else {
            $timeRange .= "<option value='{$currentTime}' disabled>{$currentTime} (Blocked)</option>";
        }

        $currentTime = date('H:i', strtotime($currentTime) + ($interval * 60));
    }

    return $timeRange;
}
?>
gdx19jrr

gdx19jrr3#

下面是在本地机器上测试的更新代码

<?php

$open_from = "09:00";
$open_to = "17:00";
$interval = "15 mins";  

$today_number = date('w');

$intervals = array(
    array(
        'day' => '4',
        'start' => '10:00',
        'end' => '12:00',
    ),
    array(
        'day' => '4',
        'start' => '12:30',
        'end' => '13:30',
    ),
    array(
        'day' => '5',
        'start' => '15:30',
        'end' => '16:00',
    ),
);  

if(!isset($_GET['date'])){                  
    $date = date('d-m-Y');          
}else{          
    $date = $_GET['date'];          
} 

$times = create_time_range($open_from, $open_to, $interval);

foreach ($times as $key => $time) { 
    $day = date('w', strtotime($date));
    $timeFrame = date('H:i', $time);
   
    foreach ($intervals as $slot) {
        if ($day == $slot['day'] && $timeFrame >= $slot['start'] && $timeFrame < $slot['end']) {
            echo 'No </br>';
        } else {
            echo 'Yes </br>';
        }
    }
}

function create_time_range($start, $end, $interval) { 

  $start_time = strtotime($start); 
  $end_time   = strtotime($end); 

  $current    = time(); 
  $add_time   = strtotime('+'.$interval, $current); 

  $diff       = $add_time-$current; 

  $times = array(); 
  while ($start_time < $end_time) { 
      $times[] = $start_time; 
      $start_time += $diff; 
  } 
  $times[] = $start_time; 
  return $times; 
  
}
8i9zcol2

8i9zcol24#

为了回答您的问题,您可以使用以下代码来实现您想要的结果:

if(!isset($_GET['date']))
    $date = date('d-m-Y');
else
    $date = $_GET['date'];

$startTime     = '09:00';
$endTime       = '17:00';
$interval      = 15; // minutes
$day           = date('N', strtotime($date));

$bookedSlots = array(
    array(
        'day' => '4',
        'start' => '10:00',
        'end' => '12:00',
    ),
    array(
        'day' => '4',
        'start' => '12:30',
        'end' => '13:30',
    ),
    array(
        'day' => '5',
        'start' => '15:30',
        'end' => '16:00',
    ),
);

function fetchTimeIntervals($startTime, $endTime, $intervalMinutes) {
    global $day,$bookedSlots;

    $htmlContent = '';

    // Convert start and end times to DateTime objects
    $start  = new DateTime($startTime);
    $end    = new DateTime($endTime);

    // Create an interval object based on the specified minutes
    $interval = DateInterval::createFromDateString($intervalMinutes.' minutes');

    // Create a date period object based on the start, end, and interval
    $period = new DatePeriod($start, $interval, $end);

    // Iterate through each interval and check each slot whether it is booked or not
    foreach ($period as $slot) {
        foreach ($bookedSlots as $subArr) {
            $isSlotBooked = false;

            $bookedSlotStartTime = new DateTime($subArr['start']);
            $bookedSlotEndTime   = new DateTime($subArr['end']);

            if($subArr['day'] == $day && $bookedSlotStartTime->getTimestamp()-$slot->getTimestamp() <= 0 && $bookedSlotEndTime->getTimestamp()-$slot->getTimestamp() > 0)
                $isSlotBooked = true;
        }

        $disableClass = ($isSlotBooked) ? 'disabled' : '';
        $slotTime     = $slot->format("H:i");
        $htmlContent .= "<option value='{$slotTime}' {$disableClass}>{$slotTime}</option>";
    }
    return $htmlContent;
}

echo $timeIntervals = fetchTimeIntervals($startTime, $endTime, $intervalMinutes);
?>

相关问题