时间范围数组问题与PHP

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

Time Range Array problems with PHP

问题

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

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

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

  1. <?php
  2. $open_from = "09:00";
  3. $open_to = "17:00";
  4. $interval = "15 mins";
  5. $today_number = date('w');
  6. $intervals = array(
  7. array(
  8. 'day' => '4',
  9. 'start' => '10:00',
  10. 'end' => '12:00',
  11. ),
  12. array(
  13. 'day' => '4',
  14. 'start' => '12:30',
  15. 'end' => '13:30',
  16. ),
  17. array(
  18. 'day' => '5',
  19. 'start' => '15:30',
  20. 'end' => '16:00',
  21. ),
  22. );
  23. if(!isset($_GET['date'])){
  24. $date = date('d-m-Y');
  25. }else{
  26. $date = $_GET['date'];
  27. }
  28. function create_time_range($start, $end, $interval) {
  29. $start_time = strtotime($start);
  30. $end_time = strtotime($end);
  31. $current = time();
  32. $add_time = strtotime('+'.$interval, $current);
  33. $diff = $add_time-$current;
  34. $times = array();
  35. while ($start_time < $end_time) {
  36. $times[] = $start_time;
  37. $start_time += $diff;
  38. }
  39. $times[] = $start_time;
  40. return $times;
  41. }
  42. $times = create_time_range($open_from, $open_to, $interval);
  43. foreach ($times as $key => $time) {
  44. $times[$key] = date($date.'H:i', $time);
  45. foreach($intervals as $key2 => $item) {
  46. if($intervals[$key2]['day']==$today_number && date('H:i',$time) == $intervals[$key2]['start'] && date('H:i',$time) == $intervals[$key2]['end']){
  47. echo "yes";
  48. }else{
  49. echo "no";
  50. }
  51. echo "<option value='".date('H:i', $timestamp)."'>".date('H:i',$time)."</option><br>";
  52. }
  53. }
  54. ?>

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

英文:

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?

  1. &lt;?php
  2. $open_from = &quot;09:00&quot;;
  3. $open_to = &quot;17:00&quot;;
  4. $interval = &quot;15 mins&quot;;
  5. $today_number = date(&#39;w&#39;);
  6. $intervals = array(
  7. array(
  8. &#39;day&#39; =&gt; &#39;4&#39;,
  9. &#39;start&#39; =&gt; &#39;10:00&#39;,
  10. &#39;end&#39; =&gt; &#39;12:00&#39;,
  11. ),
  12. array(
  13. &#39;day&#39; =&gt; &#39;4&#39;,
  14. &#39;start&#39; =&gt; &#39;12:30&#39;,
  15. &#39;end&#39; =&gt; &#39;13:30&#39;,
  16. ),
  17. array(
  18. &#39;day&#39; =&gt; &#39;5&#39;,
  19. &#39;start&#39; =&gt; &#39;15:30&#39;,
  20. &#39;end&#39; =&gt; &#39;16:00&#39;,
  21. ),
  22. );
  23. if(!isset($_GET[&#39;date&#39;])){
  24. $date = date(&#39;d-m-Y&#39;);
  25. }else{
  26. $date = $_GET[&#39;date&#39;];
  27. }
  28. function create_time_range($start, $end, $interval) {
  29. $start_time = strtotime($start);
  30. $end_time = strtotime($end);
  31. $current = time();
  32. $add_time = strtotime(&#39;+&#39;.$interval, $current);
  33. $diff = $add_time-$current;
  34. $times = array();
  35. while ($start_time &lt; $end_time) {
  36. $times[] = $start_time;
  37. $start_time += $diff;
  38. }
  39. $times[] = $start_time;
  40. return $times;
  41. }
  42. $times = create_time_range($open_from, $open_to, $interval);
  43. foreach ($times as $key =&gt; $time) {
  44. $times[$key] = date($date.&#39;H:i&#39;, $time);
  45. foreach($intervals as $key2 =&gt; $item) {
  46. 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;]){
  47. echo &quot;yes&quot;;
  48. }else{
  49. echo &quot;no&quot;;
  50. }
  51. 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;;
  52. //echo $intervals[$key][&#39;day&#39;].&quot; - &quot;.$intervals[$key][&#39;start&#39;].&quot; - &quot;.$intervals[$key][&#39;end&#39;];
  53. //echo &quot;&lt;br&gt;&quot;;
  54. }
  55. }
  56. ?&gt;

答案1

得分: 1

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

  1. if(!isset($_GET['date']))
  2. $date = date('d-m-Y');
  3. else
  4. $date = $_GET['date'];
  5. $startTime = '09:00';
  6. $endTime = '17:00';
  7. $interval = 15; // minutes
  8. $day = date('N', strtotime($date));
  9. $bookedSlots = array(
  10. array(
  11. 'day' => '4',
  12. 'start' => '10:00',
  13. 'end' => '12:00',
  14. ),
  15. array(
  16. 'day' => '4',
  17. 'start' => '12:30',
  18. 'end' => '13:30',
  19. ),
  20. array(
  21. 'day' => '5',
  22. 'start' => '15:30',
  23. 'end' => '16:00',
  24. ),
  25. );
  26. function fetchTimeIntervals($startTime, $endTime, $intervalMinutes) {
  27. global $day,$bookedSlots;
  28. $htmlContent = '';
  29. // Convert start and end times to DateTime objects
  30. $start = new DateTime($startTime);
  31. $end = new DateTime($endTime);
  32. // Create an interval object based on the specified minutes
  33. $interval = DateInterval::createFromDateString($intervalMinutes.' minutes');
  34. // Create a date period object based on the start, end, and interval
  35. $period = new DatePeriod($start, $interval, $end);
  36. // Iterate through each interval and check each slot whether it is booked or not
  37. foreach ($period as $slot) {
  38. foreach ($bookedSlots as $subArr) {
  39. $isSlotBooked = false;
  40. $bookedSlotStartTime = new DateTime($subArr['start']);
  41. $bookedSlotEndTime = new DateTime($subArr['end']);
  42. if($subArr['day'] == $day && $bookedSlotStartTime->getTimestamp()-$slot->getTimestamp() <= 0 && $bookedSlotEndTime->getTimestamp()-$slot->getTimestamp() > 0)
  43. $isSlotBooked = true;
  44. }
  45. $disableClass = ($isSlotBooked) ? 'disabled' : '';
  46. $slotTime = $slot->format("H:i");
  47. $htmlContent .= "<option value='{$slotTime}' {$disableClass}>{$slotTime}</option>";
  48. }
  49. return $htmlContent;
  50. }
  51. echo $timeIntervals = fetchTimeIntervals($startTime, $endTime, $intervalMinutes);
  52. ?>
英文:

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

  1. if(!isset($_GET[&#39;date&#39;]))
  2. $date = date(&#39;d-m-Y&#39;);
  3. else
  4. $date = $_GET[&#39;date&#39;];
  5. $startTime = &#39;09:00&#39;;
  6. $endTime = &#39;17:00&#39;;
  7. $interval = 15; // minutes
  8. $day = date(&#39;N&#39;, strtotime($date));
  9. $bookedSlots = array(
  10. array(
  11. &#39;day&#39; =&gt; &#39;4&#39;,
  12. &#39;start&#39; =&gt; &#39;10:00&#39;,
  13. &#39;end&#39; =&gt; &#39;12:00&#39;,
  14. ),
  15. array(
  16. &#39;day&#39; =&gt; &#39;4&#39;,
  17. &#39;start&#39; =&gt; &#39;12:30&#39;,
  18. &#39;end&#39; =&gt; &#39;13:30&#39;,
  19. ),
  20. array(
  21. &#39;day&#39; =&gt; &#39;5&#39;,
  22. &#39;start&#39; =&gt; &#39;15:30&#39;,
  23. &#39;end&#39; =&gt; &#39;16:00&#39;,
  24. ),
  25. );
  26. function fetchTimeIntervals($startTime, $endTime, $intervalMinutes) {
  27. global $day,$bookedSlots;
  28. $htmlContent = &#39;&#39;;
  29. // Convert start and end times to DateTime objects
  30. $start = new DateTime($startTime);
  31. $end = new DateTime($endTime);
  32. // Create an interval object based on the specified minutes
  33. $interval = DateInterval::createFromDateString($intervalMinutes.&#39; minutes&#39;);
  34. // Create a date period object based on the start, end, and interval
  35. $period = new DatePeriod($start, $interval, $end);
  36. // Iterate through each interval and check each slot whether it is booked or not
  37. foreach ($period as $slot) {
  38. foreach ($bookedSlots as $subArr) {
  39. $isSlotBooked = false;
  40. $bookedSlotStartTime = new DateTime($subArr[&#39;start&#39;]);
  41. $bookedSlotEndTime = new DateTime($subArr[&#39;end&#39;]);
  42. 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)
  43. $isSlotBooked = true;
  44. }
  45. $disableClass = ($isSlotBooked) ? &#39;disabled&#39; : &#39;&#39;;
  46. $slotTime = $slot-&gt;format(&quot;H:i&quot;);
  47. $htmlContent .= &quot;&lt;option value=&#39;{$slotTime}&#39; {$disableClass}&gt;{$slotTime}&lt;/option&gt;&quot;;
  48. }
  49. return $htmlContent;
  50. }
  51. echo $timeIntervals = fetchTimeIntervals($startTime, $endTime, $intervalMinutes);
  52. ?&gt;

答案2

得分: 0

尝试这种方式:

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

还可以为我们提供有关可能引发的错误的更多信息。最终启用调试:

  1. ini_set('display_errors', 1);
  2. ini_set('display_startup_errors', 1);
  3. error_reporting(E_ALL);

更新 1:
在 foreach 循环内部,您尝试修改您正在循环的同一个数组($times[$key]),这似乎没有意义。

英文:

Try this way:

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

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

  1. ini_set(&#39;display_errors&#39;, 1);
  2. ini_set(&#39;display_startup_errors&#39;, 1);
  3. 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

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

  1. $startTime = '09:00';
  2. $endTime = '17:00';
  3. $interval = 15; // 分钟
  4. $weekNumber = date('w');
  5. $excludedSlots = [
  6. ['day' => '4', 'start' => '10:00', 'end' => '12:00'],
  7. ['day' => '4', 'start' => '12:30', 'end' => '13:30'],
  8. ['day' => '5', 'start' => '15:30', 'end' => '16:00']
  9. ];
  10. $timeRange = generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots);
  11. echo $timeRange;
  12. function generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots) {
  13. $timeRange = '';
  14. $currentTime = $startTime;
  15. while ($currentTime <= $endTime) {
  16. $isExcluded = false;
  17. foreach ($excludedSlots as $excludedSlot) {
  18. if ($weekNumber == $excludedSlot['day'] && $currentTime >= $excludedSlot['start'] && $currentTime < $excludedSlot['end']) {
  19. $isExcluded = true;
  20. break;
  21. }
  22. }
  23. if (!$isExcluded) {
  24. $timeRange .= "<option value='{$currentTime}'>{$currentTime}</option>";
  25. } else {
  26. $timeRange .= "<option value='{$currentTime}' disabled>{$currentTime} (Blocked)</option>";
  27. }
  28. $currentTime = date('H:i', strtotime($currentTime) + ($interval * 60));
  29. }
  30. return $timeRange;
  31. }
  32. ?>
英文:

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

  1. &lt;?php
  2. $startTime = &#39;09:00&#39;;
  3. $endTime = &#39;17:00&#39;;
  4. $interval = 15; // minutes
  5. $weekNumber = date(&#39;w&#39;);
  6. $excludedSlots = [
  7. [&#39;day&#39; =&gt; &#39;4&#39;, &#39;start&#39; =&gt; &#39;10:00&#39;, &#39;end&#39; =&gt; &#39;12:00&#39;],
  8. [&#39;day&#39; =&gt; &#39;4&#39;, &#39;start&#39; =&gt; &#39;12:30&#39;, &#39;end&#39; =&gt; &#39;13:30&#39;],
  9. [&#39;day&#39; =&gt; &#39;5&#39;, &#39;start&#39; =&gt; &#39;15:30&#39;, &#39;end&#39; =&gt; &#39;16:00&#39;]
  10. ];
  11. $timeRange = generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots);
  12. echo $timeRange;
  13. function generateTimeRange($startTime, $endTime, $interval, $weekNumber, $excludedSlots) {
  14. $timeRange = &#39;&#39;;
  15. $currentTime = $startTime;
  16. while ($currentTime &lt;= $endTime) {
  17. $isExcluded = false;
  18. foreach ($excludedSlots as $excludedSlot) {
  19. if ($weekNumber == $excludedSlot[&#39;day&#39;] &amp;&amp; $currentTime &gt;= $excludedSlot[&#39;start&#39;] &amp;&amp; $currentTime &lt; $excludedSlot[&#39;end&#39;]) {
  20. $isExcluded = true;
  21. break;
  22. }
  23. }
  24. if (!$isExcluded) {
  25. $timeRange .= &quot;&lt;option value=&#39;{$currentTime}&#39;&gt;{$currentTime}&lt;/option&gt;&quot;;
  26. } else {
  27. $timeRange .= &quot;&lt;option value=&#39;{$currentTime}&#39; disabled&gt;{$currentTime} (Blocked)&lt;/option&gt;&quot;;
  28. }
  29. $currentTime = date(&#39;H:i&#39;, strtotime($currentTime) + ($interval * 60));
  30. }
  31. return $timeRange;
  32. }
  33. ?&gt;

答案4

得分: 0

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

  1. $open_from = "09:00";
  2. $open_to = "17:00";
  3. $interval = "15 分钟";
  4. $today_number = date('w');
  5. $intervals = array(
  6. array(
  7. 'day' => '4',
  8. 'start' => '10:00',
  9. 'end' => '12:00',
  10. ),
  11. array(
  12. 'day' => '4',
  13. 'start' => '12:30',
  14. 'end' => '13:30',
  15. ),
  16. array(
  17. 'day' => '5',
  18. 'start' => '15:30',
  19. 'end' => '16:00',
  20. ),
  21. );
  22. if(!isset($_GET['date'])){
  23. $date = date('d-m-Y');
  24. }else{
  25. $date = $_GET['date'];
  26. }
  27. $times = create_time_range($open_from, $open_to, $interval);
  28. foreach ($times as $key => $time) {
  29. $day = date('w', strtotime($date));
  30. $timeFrame = date('H:i', $time);
  31. foreach ($intervals as $slot) {
  32. if ($day == $slot['day'] && $timeFrame >= $slot['start'] && $timeFrame < $slot['end']) {
  33. echo '不行 </br>';
  34. } else {
  35. echo '可以 </br>';
  36. }
  37. }
  38. }
  39. function create_time_range($start, $end, $interval) {
  40. $start_time = strtotime($start);
  41. $end_time = strtotime($end);
  42. $current = time();
  43. $add_time = strtotime('+'.$interval, $current);
  44. $diff = $add_time-$current;
  45. $times = array();
  46. while ($start_time < $end_time) {
  47. $times[] = $start_time;
  48. $start_time += $diff;
  49. }
  50. $times[] = $start_time;
  51. return $times;
  52. }
英文:

Here is the updated code tested on local machine

  1. &lt;?php
  2. $open_from = &quot;09:00&quot;;
  3. $open_to = &quot;17:00&quot;;
  4. $interval = &quot;15 mins&quot;;
  5. $today_number = date(&#39;w&#39;);
  6. $intervals = array(
  7. array(
  8. &#39;day&#39; =&gt; &#39;4&#39;,
  9. &#39;start&#39; =&gt; &#39;10:00&#39;,
  10. &#39;end&#39; =&gt; &#39;12:00&#39;,
  11. ),
  12. array(
  13. &#39;day&#39; =&gt; &#39;4&#39;,
  14. &#39;start&#39; =&gt; &#39;12:30&#39;,
  15. &#39;end&#39; =&gt; &#39;13:30&#39;,
  16. ),
  17. array(
  18. &#39;day&#39; =&gt; &#39;5&#39;,
  19. &#39;start&#39; =&gt; &#39;15:30&#39;,
  20. &#39;end&#39; =&gt; &#39;16:00&#39;,
  21. ),
  22. );
  23. if(!isset($_GET[&#39;date&#39;])){
  24. $date = date(&#39;d-m-Y&#39;);
  25. }else{
  26. $date = $_GET[&#39;date&#39;];
  27. }
  28. $times = create_time_range($open_from, $open_to, $interval);
  29. foreach ($times as $key =&gt; $time) {
  30. $day = date(&#39;w&#39;, strtotime($date));
  31. $timeFrame = date(&#39;H:i&#39;, $time);
  32. foreach ($intervals as $slot) {
  33. if ($day == $slot[&#39;day&#39;] &amp;&amp; $timeFrame &gt;= $slot[&#39;start&#39;] &amp;&amp; $timeFrame &lt; $slot[&#39;end&#39;]) {
  34. echo &#39;No &lt;/br&gt;&#39;;
  35. } else {
  36. echo &#39;Yes &lt;/br&gt;&#39;;
  37. }
  38. }
  39. }
  40. function create_time_range($start, $end, $interval) {
  41. $start_time = strtotime($start);
  42. $end_time = strtotime($end);
  43. $current = time();
  44. $add_time = strtotime(&#39;+&#39;.$interval, $current);
  45. $diff = $add_time-$current;
  46. $times = array();
  47. while ($start_time &lt; $end_time) {
  48. $times[] = $start_time;
  49. $start_time += $diff;
  50. }
  51. $times[] = $start_time;
  52. return $times;
  53. }

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:

确定