前端:
后臺設置:?
代碼:
public function getBusinessHour(){// 需求單門店$data = (new StoreModel())->limit(1)->select()->toArray();$days = explode(',', $data[0]['shop_hours']);$businessHours = $days[1];// 使用 explode 分割字符串,獲取開始和結束時間list($startTime, $endTime) = explode('-', $businessHours);// 初始化結果數組$timePeriods = [];// 將開始時間轉換為時間戳$current = strtotime($startTime);// 循環直到當前時間戳等于或超過結束時間while ($current <= strtotime($endTime)) {// 計算結束時間,如果是最后一時間段,則使用原始的結束時間$end = ($current == strtotime($endTime)) ? $endTime : date('H:i', strtotime('+30 minutes', $current));// 格式化當前時間段并添加到結果數組$timePeriods[] = date('H:i', $current) . '-' . $end;// 將當前時間戳增加30分鐘$current = strtotime('+30 minutes', $current);}if (isset($days[0])) {preg_match('/(.*?)至(.*)/', $days[0], $matches);$res = [];$currentDate = date('Y-m-d H:i:s');// 使用strftime函數獲取當前是周幾的數字$weekdayNumber = date('w', strtotime($currentDate));// 定義一個數組來將數字表示的周幾轉換為中文$weekdays = ['日', '一', '二', '三', '四', '五', '六'];// 獲取當前是周幾的中文名稱switch ($matches[2]) {case '周二':$i = 2;break;case '周三':$i = 3;break;case '周四':$i = 4;break;case '周五':$i = 5;break;case '周六':$i = 6;break;case '周日':$i = 7;break;case '周一':default:$i = 1;break;}$dayOfWeek = date('N');// 計算從當前日期到周五的剩余工作日天數$remainingWorkdays = $i - $dayOfWeek + 1;for ($j = 0; $j < $remainingWorkdays; $j++) {// 獲取循環中的當前工作日$currentDay = ($dayOfWeek + $j - 1) % 7;if ($currentDay == 0) {$currentDay = 7; // 周日}// 計算具體的月和日,增加 $j 天(不包括當前天)$targetDate = date('Y-m-d', strtotime("+$j day", strtotime($currentDate)));$targetDateWeekdayNumber = date('w', strtotime($targetDate)); // 獲取 $targetDate 對應的周幾數字$targetDateWeekdayChinese = $weekdays[$targetDateWeekdayNumber]; // 獲取 $targetDate 對應的周幾中文名稱$res[$targetDate][] = $targetDate . '(' .'周'. $targetDateWeekdayChinese . ')';; // 當天的日期// 假設 $days[1] 是您要添加的數據,這里直接引用$res[$targetDate][] = $timePeriods;}return $res;}}
結果: