时间范围数组问题与PHP

huangapple go评论69阅读模式
英文:

Time Range Array problems with PHP

问题

我正在尝试编写一个预订机场时间段的应用程序,基本上有一个开放和关闭时段以及时间间隔,该应用程序然后在两个时间值之间循环并显示一个选择下拉菜单。

我需要添加一个选项,以便机场可以阻止一些时间段的预订,例如每周四上午10:00到12:00,机场关闭预订,我需要将这个功能添加到循环中,但无法使其正常工作。它需要是特定的日期(星期一至星期日),而不是特定的日期,因为机场每周的相同时间段内关闭。

这是我到目前为止的代码,有人能帮忙吗?

<?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>";  	
    }
}
?>

希望这可以帮助你解决问题。如果你有其他问题或需要进一步的帮助,请随时告诉我。

英文:

I am trying to write an application for booking a time slot at an airfield, basically there is an open and close period and time interval, the application then loops between the two time values and displays a select dropdown.

I need to build in the option for the airfield to block out time ranges, so for example every Thursday between 10:00am and 12:00pm the airfield is closed to bookings, I need to build this into the loop but cannot get it working. It needs to be day specific (Mon-Sun) rather than date specific as the airfield will be closed every specified day each week for the same time period.

Here is my code so far, can anyone help please?

&lt;?php
$open_from = &quot;09:00&quot;;
$open_to = &quot;17:00&quot;;
$interval = &quot;15 mins&quot;;	
$today_number = date(&#39;w&#39;);
$intervals = array(
array(
&#39;day&#39; =&gt; &#39;4&#39;,
&#39;start&#39; =&gt; &#39;10:00&#39;,
&#39;end&#39; =&gt; &#39;12:00&#39;,
),
array(
&#39;day&#39; =&gt; &#39;4&#39;,
&#39;start&#39; =&gt; &#39;12:30&#39;,
&#39;end&#39; =&gt; &#39;13:30&#39;,
),
array(
&#39;day&#39; =&gt; &#39;5&#39;,
&#39;start&#39; =&gt; &#39;15:30&#39;,
&#39;end&#39; =&gt; &#39;16:00&#39;,
),
);	
if(!isset($_GET[&#39;date&#39;])){			  		
$date = date(&#39;d-m-Y&#39;);  		
}else{  		
$date = $_GET[&#39;date&#39;];  		
} 
function create_time_range($start, $end, $interval) { 
$start_time = strtotime($start); 
$end_time   = strtotime($end); 
$current    = time(); 
$add_time   = strtotime(&#39;+&#39;.$interval, $current); 
$diff       = $add_time-$current; 
$times = array(); 
while ($start_time &lt; $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 =&gt; $time) { 
$times[$key] = date($date.&#39;H:i&#39;, $time); 
foreach($intervals as $key2 =&gt; $item) {
if($intervals[$key2][&#39;day&#39;]==$today_number &amp;&amp; date(&#39;H:i&#39;,$time) == $intervals[$key2][&#39;start&#39;] &amp;&amp; date(&#39;H:i&#39;,$time) == $intervals[$key2][&#39;end&#39;]){
echo &quot;yes&quot;;
}else{
echo &quot;no&quot;;
}
echo &quot;&lt;option value=&#39;&quot;.date(&#39;H:i&#39;, $timestamp).&quot;&#39;&gt;&quot;.date(&#39;H:i&#39;,$time).&quot;&lt;/option&gt;&lt;br&gt;&quot;;  	
//echo $intervals[$key][&#39;day&#39;].&quot; - &quot;.$intervals[$key][&#39;start&#39;].&quot; - &quot;.$intervals[$key][&#39;end&#39;];
//echo &quot;&lt;br&gt;&quot;;
}
}
?&gt;

答案1

得分: 1

作为回应您的问题,您可以使用以下代码来实现您的期望结果:

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);
?>
英文:

In response to your question, you can achieve your desire result by using the following code:

if(!isset($_GET[&#39;date&#39;]))
$date = date(&#39;d-m-Y&#39;);
else
$date = $_GET[&#39;date&#39;];
$startTime     = &#39;09:00&#39;;
$endTime       = &#39;17:00&#39;;
$interval      = 15; // minutes
$day           = date(&#39;N&#39;, strtotime($date));
$bookedSlots = array(
array(
&#39;day&#39; =&gt; &#39;4&#39;,
&#39;start&#39; =&gt; &#39;10:00&#39;,
&#39;end&#39; =&gt; &#39;12:00&#39;,
),
array(
&#39;day&#39; =&gt; &#39;4&#39;,
&#39;start&#39; =&gt; &#39;12:30&#39;,
&#39;end&#39; =&gt; &#39;13:30&#39;,
),
array(
&#39;day&#39; =&gt; &#39;5&#39;,
&#39;start&#39; =&gt; &#39;15:30&#39;,
&#39;end&#39; =&gt; &#39;16:00&#39;,
),
);
function fetchTimeIntervals($startTime, $endTime, $intervalMinutes) {
global $day,$bookedSlots;
$htmlContent = &#39;&#39;;
// 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.&#39; minutes&#39;);
// 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[&#39;start&#39;]);
$bookedSlotEndTime   = new DateTime($subArr[&#39;end&#39;]);
if($subArr[&#39;day&#39;] == $day &amp;&amp; $bookedSlotStartTime-&gt;getTimestamp()-$slot-&gt;getTimestamp() &lt;= 0 &amp;&amp; $bookedSlotEndTime-&gt;getTimestamp()-$slot-&gt;getTimestamp() &gt; 0)
$isSlotBooked = true;
}
$disableClass = ($isSlotBooked) ? &#39;disabled&#39; : &#39;&#39;;
$slotTime     = $slot-&gt;format(&quot;H:i&quot;);
$htmlContent .= &quot;&lt;option value=&#39;{$slotTime}&#39; {$disableClass}&gt;{$slotTime}&lt;/option&gt;&quot;;
}
return $htmlContent;
}
echo $timeIntervals = fetchTimeIntervals($startTime, $endTime, $intervalMinutes);
?&gt;

答案2

得分: 0

尝试这种方式:

$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]),这似乎没有意义。

英文:

Try this way:

$interval = &quot;15 minutes&quot;;
...
$current    = time();
add_time   = strtotime($current.&#39;+&#39;.$interval);

Also can you guve us more info about eventual error throwed. Eventually enable debugging:

ini_set(&#39;display_errors&#39;, 1);
ini_set(&#39;display_startup_errors&#39;, 1);
error_reporting(E_ALL);

UPDATE 1<br/>
Ok inside the foreach you try to modify the same array you are looping ($times[$key]) and doesn't seems to have sense.

答案3

得分: 0

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

$startTime     = '09:00';
$endTime       = '17:00';
$interval      = 15; // 分钟
$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;
}
?>
英文:

Check this out. I hope I've understood your problem right and provided the desired solution.

&lt;?php
$startTime     = &#39;09:00&#39;;
$endTime       = &#39;17:00&#39;;
$interval      = 15; // minutes
$weekNumber    = date(&#39;w&#39;);
$excludedSlots = [
[&#39;day&#39; =&gt; &#39;4&#39;, &#39;start&#39; =&gt; &#39;10:00&#39;, &#39;end&#39; =&gt; &#39;12:00&#39;],
[&#39;day&#39; =&gt; &#39;4&#39;, &#39;start&#39; =&gt; &#39;12:30&#39;, &#39;end&#39; =&gt; &#39;13:30&#39;],
[&#39;day&#39; =&gt; &#39;5&#39;, &#39;start&#39; =&gt; &#39;15:30&#39;, &#39;end&#39; =&gt; &#39;16:00&#39;]
];
$timeRange = generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots);
echo $timeRange;
function generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots) {
$timeRange = &#39;&#39;;
$currentTime = $startTime;
while ($currentTime &lt;= $endTime) {
$isExcluded = false;
foreach ($excludedSlots as $excludedSlot) {
if ($weekNumber == $excludedSlot[&#39;day&#39;] &amp;&amp; $currentTime &gt;= $excludedSlot[&#39;start&#39;] &amp;&amp; $currentTime &lt; $excludedSlot[&#39;end&#39;]) {
$isExcluded = true;
break;
}
}
if (!$isExcluded) {
$timeRange .= &quot;&lt;option value=&#39;{$currentTime}&#39;&gt;{$currentTime}&lt;/option&gt;&quot;;
} else {
$timeRange .= &quot;&lt;option value=&#39;{$currentTime}&#39; disabled&gt;{$currentTime} (Blocked)&lt;/option&gt;&quot;;
}
$currentTime = date(&#39;H:i&#39;, strtotime($currentTime) + ($interval * 60));
}
return $timeRange;
}
?&gt;

答案4

得分: 0

以下是在本地机器上测试过的更新后的代码:


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

$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 '不行 </br>';
        } else {
            echo '可以 </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; 
  
}

英文:

Here is the updated code tested on local machine

&lt;?php
$open_from = &quot;09:00&quot;;
$open_to = &quot;17:00&quot;;
$interval = &quot;15 mins&quot;;  
$today_number = date(&#39;w&#39;);
$intervals = array(
array(
&#39;day&#39; =&gt; &#39;4&#39;,
&#39;start&#39; =&gt; &#39;10:00&#39;,
&#39;end&#39; =&gt; &#39;12:00&#39;,
),
array(
&#39;day&#39; =&gt; &#39;4&#39;,
&#39;start&#39; =&gt; &#39;12:30&#39;,
&#39;end&#39; =&gt; &#39;13:30&#39;,
),
array(
&#39;day&#39; =&gt; &#39;5&#39;,
&#39;start&#39; =&gt; &#39;15:30&#39;,
&#39;end&#39; =&gt; &#39;16:00&#39;,
),
);  
if(!isset($_GET[&#39;date&#39;])){                  
$date = date(&#39;d-m-Y&#39;);          
}else{          
$date = $_GET[&#39;date&#39;];          
} 
$times = create_time_range($open_from, $open_to, $interval);
foreach ($times as $key =&gt; $time) { 
$day = date(&#39;w&#39;, strtotime($date));
$timeFrame = date(&#39;H:i&#39;, $time);
foreach ($intervals as $slot) {
if ($day == $slot[&#39;day&#39;] &amp;&amp; $timeFrame &gt;= $slot[&#39;start&#39;] &amp;&amp; $timeFrame &lt; $slot[&#39;end&#39;]) {
echo &#39;No &lt;/br&gt;&#39;;
} else {
echo &#39;Yes &lt;/br&gt;&#39;;
}
}
}
function create_time_range($start, $end, $interval) { 
$start_time = strtotime($start); 
$end_time   = strtotime($end); 
$current    = time(); 
$add_time   = strtotime(&#39;+&#39;.$interval, $current); 
$diff       = $add_time-$current; 
$times = array(); 
while ($start_time &lt; $end_time) { 
$times[] = $start_time; 
$start_time += $diff; 
} 
$times[] = $start_time; 
return $times; 
}

huangapple
  • 本文由 发表于 2023年6月15日 20:31:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定