whereBetween('created_at', $betweenTimes)->orderBy('id')->get(); /** @var self $record */ foreach ($records as $record) { $times = array_map(function ($time) { return preg_replace("/(\d+):(\d+):\d+$/", "$1:$2", $time); }, $record->times); if (isset($array[$record->date])) { $array[$record->date] = array_merge($array[$record->date], $times); } else { $array[$record->date] = $times; } } // foreach ($array as $date => $times) { $times = array_values(array_filter(array_unique($times))); $inOrder = []; foreach ($times as $key => $time) { $inOrder[$key] = strtotime("2022-01-01 {$time}"); } array_multisort($inOrder, SORT_ASC, $times); $array[$date] = $times; } // return $array; } /** * 时间收集 * @param string $data 日期 * @param array $times 签到时间数组 * @param string|null $shiftStart 班次开始时间(如 "09:00"),用于判断跨天 * @return \Illuminate\Support\Collection */ public static function atCollect($data, $times, $shiftStart = null) { $shiftStartMinutes = null; if ($shiftStart) { $parts = explode(':', $shiftStart); $shiftStartMinutes = intval($parts[0]) * 60 + intval($parts[1]); } $sameTimes = array_map(function($time) use ($data, $shiftStartMinutes) { $parts = explode(':', $time); $timeMinutes = intval($parts[0]) * 60 + intval($parts[1]); // 如果签到时间早于班次开始时间,视为跨天打卡(属于次日凌晨) $targetDate = $data; if ($shiftStartMinutes !== null && $timeMinutes < $shiftStartMinutes) { $targetDate = date("Y-m-d", strtotime($data . " +1 day")); } return [ "datetime" => "{$targetDate} {$time}", "timestamp" => strtotime("{$targetDate} {$time}") ]; }, $times); return collect($sameTimes); } /** * 签到时段 * @param array $times * @param int $diff 多长未签到算失效(秒) * @return array */ public static function atSection($times, $diff = 3600) { $start = ""; $end = ""; $array = []; foreach ($times as $time) { $time = preg_replace("/(\d+):(\d+):\d+$/", "$1:$2", $time); if (empty($start)) { $start = $time; continue; } if (empty($end)) { $end = $time; continue; } if (strtotime("2022-01-01 {$time}") - strtotime("2022-01-01 {$end}") > $diff) { $array[] = [$start, $end]; $start = $time; $end = ""; continue; } $end = $time; } if ($start) { $array[] = [$start, $end]; } return $array; } }