前端:
后台设置:
代码:
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;}}
结果: