英文:
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?
<?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>";
}
}
?>
答案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['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);
?>
答案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 = "15 minutes";
...
$current = time();
add_time = strtotime($current.'+'.$interval);
Also can you guve us more info about eventual error throwed. Eventually enable debugging:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 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.
<?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;
}
?>
答案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
<?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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论