PHP 自提时间

前端:

后台设置: 

代码:

 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;
        }
    }

结果:

 B:  
 

  public function getBusinessHour()
    {
        $store = (new StoreModel())->field('id,start_time,end_time,status')->find(StoreModel::value('id'));
        if (!$store || $store['status']) {
            throw new Exception('门店正在休息中');
        }
        $startTime = $store['start_time'];
        $endTime = $store['end_time'];
        // 初始化结果数组
        $timePeriods = [];
        // 将开始时间转换为时间戳
        $current = strtotime($startTime);
        $endCur = strtotime($endTime);

        // 获取每天营业时间的每半小时数组
        while ($current <= $endCur) {
            // 计算结束时间,如果是最后一时间段,则使用原始的结束时间

            // 格式化当前时间段并添加到结果数组
            $timePeriods[] = date('H:i', $current);

            // 将当前时间戳增加30分钟
            $current = strtotime('+30 minutes', $current);
        }

        $week = date('w');
        $weekdays = ['日', '一', '二', '三', '四', '五', '六'];
        $curdays = ['今日', '明天', '后天'];
        $dayArr = [];
        if (time() > strtotime($startTime)) {
            for ($i = 0; $i < 3; $i++) {
                $dayItem = [];
                foreach ($timePeriods as $timePeriod) {
                    $next_time = strtotime("+{$i} days", strtotime($timePeriod));
                    if (time() < $next_time) {
                        $dayItem[] = ["value" => $next_time, 'label' => $timePeriod, 'date' => dateFormat($next_time)];
                    }
                }

                if ($dayItem) {
                    $dayArr[] = [
                        'name' => $curdays[$i] . '(周' . $weekdays[dateFormat(strtotime("+{$i} days"), 'w') ]. ')',
                        'timeList' => $dayItem
                    ];
                }
            }
        }

        return $dayArr;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值