亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據庫中的時間段被預訂 php 代碼復制而不是跳過

數據庫中的時間段被預訂 php 代碼復制而不是跳過

PHP
慕田峪4524236 2023-04-28 17:43:33
我正在使用 Fullcalendar 4 創建一個預訂系統,供客戶預訂設備維修的標注時段。到目前為止,在此處用戶的幫助下,我已經設法讓日歷對象將預訂的時段填充到日歷中,而且我幾乎也使用單獨的 eventSouce 并在 php 上將其添加到免費的未預訂時段中服務器端一天中哪些時段被占用并跳過它們。$cDate是 Fullcalendar 發送的請求的 DateTime 轉換開始日期。$eDate是 Fullcalendar 發送的請求的 DateTime 轉換結束日期。$daysInRange是一個 datediff 變量,我在頁面上進一步計算它以查看 $cDate 和 $eDate 之間有多少天。典型的 url 如下來自 ajax 請求:getBookings.php?start=2020-06-05T00%3A00%3A00&end=2020-06-05T00%3A00%3A00&timeZone=Europe%2FLondon$bookingStart = new DateTime( $_GET[ 'start' ] );$bookingEnd = new DateTime( $_GET[ 'end' ] );//Get The Start Date/Time seperately and End Date/Time Seperately.$startdate = date_format( $bookingStart, 'Y-m-d' );$starttime = date_format( $bookingStart, 'H:i:s' );$enddate = date_format( $bookingEnd, 'Y-m-d' );$endtime = date_format( $bookingEnd, 'H:i:s' );上面的代碼可以工作,但是例如,如果 2020-06-05 在 09:00:00 - 11:00:00 有一個時段,范圍是 2020-06-01 到 2020-06-08 那么我在 5 號上午 9 點到上午 11 點預訂了紅色時段,綠色時段也是免費時段。我不知道為什么我得到這個..
查看完整描述

1 回答

?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

我接近它的方式是:

  1. 在 1 天內創建一系列可用插槽。

  2. 在請求的日期范圍內創建一個新的可用時段數組,創建一個包含字段datestart、endisbooked的新數組。

    現在我有一個數組,其中包含整個請求日期范圍的槽時間和日期,我面臨的最后一個問題是如何在 foreach 循環中完成它并更改原始值?我偶然發現了 StackOverflow 上的另一個答案,它為我回答了這個問題,因此,我能夠在最終數組上執行最終的 foreach 循環,并遍歷每個數據庫結果以檢查它是否與 foreach 顯示的時間段上的日期匹配然后將isbooked標志設置為 true。

最后!我有一組工作代碼可以創建我需要的 JSON 返回值。沒有重復,只有免費插槽和預訂插槽很好地坐在一起。

我的最終代碼如下:

? ? for ( $i = 0; $i <= $daysInRange; $i++ ) {


? ? //for the current date we need to go through each slot.?

? ? foreach ( $freeSlots as $slot ) {


? ? ? ? $slotStart = new DateTime( $slot[ 'start' ] );

? ? ? ? $slotEnd = new DateTime( $slot[ 'end' ] );




? ? ? ? ? ? ? ? $aSlot[ 'date' ] = $cDate->format( "Y-m-d" );

? ? ? ? ? ? ? ? $aSlot[ 'start' ] = $slotStart->format( "H:i:s" );

? ? ? ? ? ? ? ? $aSlot[ 'end' ] = $slotEnd->format( "H:i:s" );

? ? ? ? ? ? ? ? $aSlot[ 'isbooked' ] = false;

? ? ? ? ? ? ? ? $allSlots[] = $aSlot;? ?



? ? }




? ? //Now add 1 day to the cDate and then check if its greater than the eDate

? ? $cDate->modify( '+1 Day' );

? ? if ( $cDate > $eDate ) {

? ? ? ? break;

? ? }


}

//var_export($allSlots);

#check new array against database and mark booked slots?

foreach($allSlots as &$slot){


? ? foreach($bookingResult as $booking) {


? ? ? ? if($booking['bookingdate'] == $slot['date'] && $booking['bookingstarttime'] == $slot['start'] && $booking['bookingendtime'] == $slot['end']){

? ? ? ? ? ? $slot['isbooked'] = true;

? ? ? ? }


? ? }


}

//Now booked slots are marked we can now create the JSON.

foreach ( $allSlots as $slot ) {


? ? $slotStart = new DateTime( $slot[ 'start' ] );

? ? $slotEnd = new DateTime( $slot[ 'end' ] );

? ? $slotDate = new DateTime( $slot[ 'date' ] );



? ? if ( $slot[ 'isbooked' ] == false ) {

? ? ? ? $bookingsAsJSON[ 'title' ] = 'Unbooked Timeslot';

? ? ? ? $bookingsAsJSON[ 'start' ] = $slotDate->format("Y-m-d"). ' ' . $slotStart->format( "H:i:s" );

? ? ? ? $bookingsAsJSON[ 'end' ] = $slotDate->format( "Y-m-d" ) . ' ' . $slotEnd->format( "H:i:s" );

? ? ? ? $bookingsAsJSON[ 'extendedProps' ][ 'bookingActualDate' ] = $slotDate->format( "Y-m-d" );

? ? ? ? $bookingsAsJSON[ 'extendedProps' ][ 'bookingActualStartTime' ] = $slotStart->format( "H:i:s" );

? ? ? ? $bookingsAsJSON[ 'extendedProps' ][ 'bookingActualEndTime' ] = $slotEnd->format( "H:i:s" );

? ? ? ? $calendarEvents[] = $bookingsAsJSON;

? ? }

}

日歷上的輸出如下所示:

http://img1.sycdn.imooc.com/644b95710001961b06530367.jpg

查看完整回答
反對 回復 2023-04-28
  • 1 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號