产品需求,有一个开放日的活动,时间是多选,且一个时间不能超过24小时,不能跨天,不能相等,后台没问题了,接口调取数据的时候麻烦点,因为刚开始时间存的json字段,后面改为添加一个时间表
时间字段有:stime:开始时间,etime:结束时间
// 接收的时间因为前台可以切换月份查看数据
$deta = $this->request->post('deta'); // 2023-12-10
// 转为年月
$dateParam = date('Y-m',strtotime($deta));
$currentMonthStart = $dateParam;
$currentMonthStart = date('Y-m-d 00:00:00', strtotime($currentMonthStart));
$currentMonthEnd = date('Y-m-t 23:59:59', strtotime($currentMonthStart));
// 注释的内容是上一测试的方法
$result = opmodel::alias('a')->join('opendaytime op', 'op.openday_id = a.id', 'left')->where(['a.switch'=>1,'op.stime'=>['>',$currentMonthStart],'op.etime'=>['<',$currentMonthEnd]])// 将 GROUP_CONCAT(DATE_FORMAT(op.stime, "%Y-%m-%d"))转换为年月日
// ->field('a.*,GROUP_CONCAT(op.id) as opid,CONCAT("[", GROUP_CONCAT(JSON_OBJECT( "etime", DATE_FORMAT(op.etime, "%Y-%m-%d %H:%i:%s"),"stime", DATE_FORMAT(op.stime, "%Y-%m-%d %H:%i:%s"))), "]") as optime')->field('a.*,op.id as opid,op.stime,op.etime,op.openday_id')
// ->group('a.id')->order('stime')
// ->fetchSql()->paginate($limit,$pages)->toArray();
// 根据openday_id将相同的数据归为一个数组$newData = [];foreach ($result['data'] as $item) {$opendayId = $item['openday_id'];if (!isset($newData[$opendayId])) {$newData[$opendayId] = ['id'=> $item['id'],'openday_id' => $opendayId,'name' => $item['name'],'activityimage' => $item['activityimage'],'school_id'=> $item['school_id'],'activityaddress'=> $item['activityaddress'],'quota'=> $item['quota'],'weigh'=> $item['weigh'],'opid'=> $item['opid'],'optime' => [],];}$newData[$opendayId]['optime'][] = ['stime' => $item['stime'],'etime' => $item['etime'],];}// 当前时间$currentTimestamp = strtotime(date('Y-m-d 00:00:00'));// 循环处理时间,添加未来时间futuretime和过去时间pasttimeforeach ($newData as &$activity) {$activity['futuretime'] = ''; // 未来时间$activity['pasttime'] = []; // 过去时间foreach ($activity['optime'] as &$optime) {$startTime = strtotime($optime['stime']);if ($startTime < strtotime('+1 week', $currentTimestamp) && $startTime >= $currentTimestamp ) {if (empty($activity['futuretime'])) {$activity['futuretime'] = $startTime;}}else{$activity['pasttime'][] = $startTime;}// $ymd 暂未使用$ymd = date('Y-m-d', strtotime($optime['stime']));$_ymd = date('Y-m', strtotime($optime['stime']));$hm = date('H:i', strtotime($optime['stime'])) .'-'.date('H:i', strtotime($optime['etime']));if (!$activity['currentmonth']) {$activity['currentmonth'] = explode('-', $_ymd);}// 特殊需求 展示时间$activity['ymdhm'][] = ['ymd' => $_ymd,'hm' => $hm,];}// 前台点击查看的月份$activity['cktime'] = date('Y-m',strtotime($activity['optime'][0]['stime']));// 统计当前月份的场次$activity['counttime'] = count($activity['ymdhm']);$activity['school_name'] = $activity['school_id'];// 可去可不去unset($activity['openjson']);// 如果futuretime没有数据,就将最后一个日期放入if (empty($activity['futuretime'])) {$lastOptime = end($activity['optime']);$lastStartTime = strtotime($lastOptime['stime']);$activity['futuretime'] = $lastStartTime;}}// 根据字段排序usort($newData, function($a, $b) use ($currentTimestamp) {if ($a['futuretime'] < $currentTimestamp) {return 1;} elseif ($b['futuretime'] < $currentTimestamp) {return -1;} else {return $a['futuretime'] - $b['futuretime'];}});$result['data'] = $newData;
打印结果
array:6 [0 => array:17 ["id" => 26"openday_id" => 26"name" => "活动02""activityimage" => "活动图片""school_id" => "学校名称""activityaddress" => "地址""quota" => "10000""weigh" => 26"opid" => 30"optime" => array:2 [0 => array:3 ["stime" => "2023-12-13 09:00:00""etime" => "2023-12-13 17:00:00"]1 => array:3 ["stime" => "2023-12-14 09:00:00""etime" => "2023-12-14 17:00:00"]]"futuretime" => 1702515600"pasttime" => array:1 [0 => 1702429200]"currentmonth" => array:2 [0 => "2023"1 => "12"]"ymdhm" => array:2 [0 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]1 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]]"cktime" => "2023-12""counttime" => 2"school_name" => "学校名称"]1 => array:17 ["id" => 27"openday_id" => 27"name" => "活动03""activityimage" => "活动图片""school_id" => "学校名称""activityaddress" => "地址""quota" => "10000""weigh" => 27"opid" => 32"optime" => array:3 [0 => array:3 ["stime" => "2023-12-15 09:00:00""etime" => "2023-12-15 00:00:00"]1 => array:3 ["stime" => "2023-12-16 09:00:00""etime" => "2023-12-16 17:00:00"]2 => array:3 ["stime" => "2023-12-17 09:00:00""etime" => "2023-12-17 17:00:00"]]"futuretime" => 1702602000"pasttime" => []"currentmonth" => array:2 [0 => "2023"1 => "12"]"ymdhm" => array:3 [0 => array:2 ["ymd" => "2023-12""hm" => "09:00-00:00"]1 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]2 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]]"cktime" => "2023-12""counttime" => 3"school_name" => "学校名称"]2 => array:17 ["id" => 29"openday_id" => 29"name" => "活动05""activityimage" => "活动图片""school_id" => "学校名称""activityaddress" => "地址""quota" => "10000""weigh" => 29"opid" => 36"optime" => array:1 [0 => array:3 ["stime" => "2023-12-18 09:00:00""etime" => "2023-12-18 17:00:00"]]"futuretime" => 1702861200"pasttime" => []"currentmonth" => array:2 [0 => "2023"1 => "12"]"ymdhm" => array:1 [0 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]]"cktime" => "2023-12""counttime" => 1"school_name" => "学校名称"]3 => & array:17 ["id" => 30"openday_id" => 30"name" => "活动06""activityimage" => "活动图片""school_id" => "学校名称""activityaddress" => "地址""quota" => "10000""weigh" => 30"opid" => 37"optime" => array:1 [0 => & array:3 ["stime" => "2023-12-19 09:00:00""etime" => "2023-12-19 17:00:00"]]"futuretime" => 1702947600"pasttime" => []"currentmonth" => array:2 [0 => "2023"1 => "12"]"ymdhm" => array:1 [0 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]]"cktime" => "2023-12""counttime" => 1"school_name" => "学校名称"]4 => array:17 ["id" => 25"openday_id" => 25"name" => "活动01""activityimage" => "活动图片""school_id" => "学校名称""activityaddress" => "地址""quota" => "10000""weigh" => 25"opid" => 29"optime" => array:1 [0 => array:3 ["stime" => "2023-12-13 09:00:00""etime" => "2023-12-13 17:00:00"]]"futuretime" => 1702429200"pasttime" => array:1 [0 => 1702429200]"currentmonth" => array:2 [0 => "2023"1 => "12"]"ymdhm" => array:1 [0 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]]"cktime" => "2023-12""counttime" => 1"school_name" => "学校名称"]5 => array:17 ["id" => 28"openday_id" => 28"name" => "测试活动04-测试已过时间""activityimage" => "活动图片""school_id" => "学校名称""activityaddress" => "地址""quota" => "10000""weigh" => 28"opid" => 35"optime" => array:2 [0 => array:3 ["stime" => "2023-12-08 09:00:00""etime" => "2023-12-08 17:00:00"]1 => array:3 ["stime" => "2023-12-09 09:00:00""etime" => "2023-12-09 17:00:00"]]"futuretime" => 1702083600"pasttime" => array:2 [0 => 17019972001 => 1702083600]"currentmonth" => array:2 [0 => "2023"1 => "12"]"ymdhm" => array:2 [0 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]1 => array:2 ["ymd" => "2023-12""hm" => "09:00-17:00"]]"cktime" => "2023-12""counttime" => 2"school_name" => "学校名称"]
]