PHP에서 date_sunset ()은 날짜 및 지리적 위치에 따라 일몰 시간을 계산하는 매우 실용적인 기능입니다. 그러나 date_sunset () 만 사용하면 종종 일몰 시간 만 반환하는 것과 같은 제한된 정보 만 얻습니다. 보다 포괄적 인 선샤인 정보 (예 : 일출, 일몰, 일요일, 햇빛 기간 등)를 얻으려면 정의 기능 Sun_Info ()를 캡슐화하여 날짜 _sun_info () 및 date_sunset () 의 기능을 풍부한 태양 관련 데이터에 출력 할 수 있습니다.
아래에서는 Sun_Info () 함수를 구현하고 Date_Sunset ()을 결합하여 자세한 선샤인 정보를 얻고 출력하는 방법을 보여줍니다.
먼저 두 가지 주요 기능의 사용을 간단히 소개합니다.
date_sunset () : 지정된 시간과 지리적 위치의 일몰 시간을 얻습니다.
date_sun_info () : Sunrise, Sunset, Dusk, Dawn, Sun Noon 등과 같은 자세한 정보가 포함 된 배열을 반환합니다.
date_sun_info ()는 보다 포괄적 인 기능을 가지고 있지만 date_sunset () 의 구성 가능성을 사용하여 더 많은 세분화 된 데이터를 얻을 수 있습니다.
날짜, 경도, 위도 및 시간대가 매개 변수로 날짜, 경도, 위도 및 시간대를 취하는 함수 를 작성하고 다음 정보를 포함하는 구조화 된 배열을 반환합니다.
일출 시간
일몰 시간
햇빛 길이 (단위 : 분)
태양은 정오 시간입니다
황혼 시간
새벽 시간
function sun_info($date, $latitude, $longitude, $timezone = 'UTC') {
$timestamp = strtotime($date);
$sunData = date_sun_info($timestamp, $latitude, $longitude);
$sunset = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
$sunrise = date_sunrise($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
$daylightMinutes = round(($sunset - $sunrise) / 60);
// 지정된 시간대 형식의 출력으로 변환합니다
$dtZone = new DateTimeZone($timezone);
$format = function($ts) use ($dtZone) {
if (!$ts || $ts < 0) return 'N/A';
$dt = new DateTime("@$ts");
$dt->setTimezone($dtZone);
return $dt->format('H:i:s');
};
return [
'sunrise' => $format($sunData['sunrise']),
'sunset' => $format($sunData['sunset']),
'solar_noon' => $format($sunData['solar_noon']),
'civil_dawn' => $format($sunData['civil_twilight_begin']),
'civil_dusk' => $format($sunData['civil_twilight_end']),
'day_length' => $daylightMinutes . ' 분',
];
}
2025 년 4 월 26 일 베이징에서 Rizhao 정보를 얻으려면 다음과 같이 부를 수 있습니다.
$info = sun_info('2025-04-26', 39.9042, 116.4074, 'Asia/Shanghai');
echo "베이징 오늘의 태양 정보:<br>";
foreach ($info as $label => $value) {
echo ucfirst(str_replace('_', ' ', $label)) . ': ' . $value . '<br>';
}
출력 예제는 다음과 같습니다.
Beijing 오늘의 태양 정보:
Sunrise: 05:21:32
Sunset: 18:57:20
Solar noon: 12:09:26
Civil dawn: 04:53:10
Civil dusk: 19:25:42
Day length: 456 분
이러한 햇빛 정보를보다 직관적으로 표시하기 위해 JavaScript 또는 차트 라이브러리를 결합하여 출력 효과를 더욱 아름답게 할 수 있습니다. 예를 들어 페이지의 링크를 통합 할 수도 있습니다.
echo '<a href="https://m66.net/sun?lat=39.9&lon=116.4&date=2025-04-26">더 많은 Rizhao 세부 사항을 참조하십시오</a>';