1585670400 * @param array $times * @param bool $isWithTime 是否包含时间 * @return array */ function between_time(array $times, bool $isWithTime = false): array { foreach ($times as &$time) { $time = trim($time, '"'); $time = str2date($time, $isWithTime); } return ['start_time' => current($times), 'end_time' => next($times)]; } } if (!function_exists("str2date")) { /** * 日期转换时间戳 * 例如: 2020-04-01 08:15:08 => 1585670400 * @param string $date * @param bool $isWithTime 是否包含时间 * @return int */ function str2date(string $date, bool $isWithTime = false): int { if (!$isWithTime) { $date = date('Y-m-d', strtotime($date)); } return strtotime($date); } } if (!function_exists("v_valid")) { function v_valid(array $rules, array|string $input = '', ?callable $callable = null) { if (is_string($input)) { $type = trim($input, '.') ?: 'get'; $input = request()->$type(); } [$data, $rule, $info] = [[], [], []]; foreach ($rules as $name => $message) if (is_numeric($name)) { [$name, $alias] = explode('#', $message . '#'); $data[$name] = $input[($alias ?: $name)] ?? null; } elseif (!str_contains($name, '.')) { $data[$name] = $message; } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) { [, $_key, $_rule, $alias] = $matches; if (in_array($_rule, ['value', 'default'])) { if ($_rule === 'value') $data[$_key] = $message; elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message; } else { $info[explode(':', $name)[0]] = $message; $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null); $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule; } } $validate = new Validate(); if ($validate->rule($rule)->message($info)->check($data)) { return $data; } elseif (is_callable($callable)) { return call_user_func($callable, $validate->getError(), $data); } else { return $validate->getError(); } } } if (!function_exists("pageFormat")) { /** * @param $data * @param int $size * @return array {page:"",pageSize:"",rows:[],total:""} */ function pageFormat($data, int $size = 10): array { if (empty($data)) return []; return [ 'total' => $data->total(), 'page' => $data->currentPage(), 'pageSize' => $size, 'rows' => $data->items() ]; } }