1 回答

TA貢獻1828條經驗 獲得超3個贊
我接近它的方式是:
在 1 天內創建一系列可用插槽。
在請求的日期范圍內創建一個新的可用時段數組,創建一個包含字段date、start、end、isbooked的新數組。
現在我有一個數組,其中包含整個請求日期范圍的槽時間和日期,我面臨的最后一個問題是如何在 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;
? ? }
}
日歷上的輸出如下所示:
- 1 回答
- 0 關注
- 125 瀏覽
添加回答
舉報