'Y-m-d', 'date_' => 'Y_m_d', 'datetime' => 'Y-m-d H:i:s', 'date_time' => 'Y-m-d H:i' }; return date($format, $times); } } if (!function_exists('get_select_data')) { function get_select_data( array $data = [], bool $is_all = false, string $str1 = 'label', string $str2 = 'value' ): array { $newData = []; if ($is_all) { $newData[] = [$str1 => __('admin.all'), $str2 => '']; } foreach ($data as $key => $value) { $newData[] = [ $str1 => $value, $str2 => $key ]; } return $newData; } } if (!function_exists('get_ratio')) { function get_ratio($max, $min, $is_unit = true): string { $ratio = 0; if ($max > 0 && $min > 0) { $ratio = round(($min / $max) * 100, 2); } if ($is_unit) { $ratio .= '%'; } return $ratio; } } if (!function_exists('get_time_difference')) { function get_time_difference(string $targetTime): array { try { $target = new DateTime($targetTime); $now = new DateTime(); // 计算差值 $interval = $now->diff($target); $totalDays = $interval->days; $hours = $interval->h; $minutes = $interval->i; $seconds = $interval->s; // 判断是过去还是未来 $isFuture = $now < $target; return [ 'days' => $totalDays, 'hours' => $hours, 'minutes' => $minutes, 'seconds' => $seconds, 'is_future' => $isFuture, 'formatted' => sprintf( "%s%d天 %02d时 %02d分 %02d秒", $isFuture ? '' : '-', $totalDays, $hours, $minutes, $seconds ) ]; } catch (Exception $e) { return [ 'error' => '时间格式错误: ' . $e->getMessage() ]; } } } if (!function_exists('get_image_url')) { function get_image_url($image_url): string { return $image_url ? env('APP_URL') . $image_url : ''; } } if (!function_exists('set_space_underline')) { function set_space_underline($str): string { return str_replace(' ', '_', $str); } } // 返回年月所有天 if (!function_exists('get_year_month_days')) { function get_year_month_days($year_month): array { $year_month_times = strtotime($year_month); if (!$year_month_times) { return []; } $monthTimes = strtotime('+1 month', $year_month_times); $monthEndDay = date('d', strtotime('-1 day', $monthTimes)); $dayArr = []; for ($i = 1; $i <= $monthEndDay; $i++) { $dayArr[] = strlen($i) > 1 ? $i : '0' . $i; } return $dayArr; } }