(PHP 5 >= 5.1.2, PHP 7, PHP 8)
date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end
timestamp
Unix timestamp.
latitude
Latitude in degrees.
longitude
Longitude in degrees.
Returns an array whose structure is detailed in the following list:
sunrise
sunset
transit
civil_twilight_begin
sunrise
.
civil_twilight_end
sunset
.
nautical_twilight_begin
civil_twilight_begin
.
nautical_twilight_end
civil_twilight_end
.
astronomical_twilight_begin
nautical_twilight_begin
.
astronomical_twilight_end
nautical_twilight_end
.
The values of the array elements are either UNIX timestamps, false
if the
sun is below the respective zenith for the whole day, or true
if the sun is
above the respective zenith for the whole day.
版本 | 说明 |
---|---|
7.2.0 | The calculation was fixed with regards to local midnight instead of local noon, which changes the results slightly. |
示例 #1 A date_sun_info() example
<?php
$sun_info = date_sun_info(strtotime("2006-12-12"), 31.7667, 35.2333);
foreach ($sun_info as $key => $val) {
echo "$key: " . date("H:i:s", $val) . "\n";
}
?>
以上示例会输出:
sunrise: 05:52:11 sunset: 15:41:21 transit: 10:46:46 civil_twilight_begin: 05:24:08 civil_twilight_end: 16:09:24 nautical_twilight_begin: 04:52:25 nautical_twilight_end: 16:41:06 astronomical_twilight_begin: 04:21:32 astronomical_twilight_end: 17:12:00
示例 #2 Polar night, with some processing
<?php
$tz = new \DateTimeZone('America/Anchorage');
$si = date_sun_info(strtotime("2022-12-21"), 70.21, -148.51);
foreach ($si as $key => $value) {
echo
match ($value) {
true => 'always',
false => 'never',
default => date_create("@{$value}")->setTimeZone($tz)->format( 'H:i:s T' ),
},
": {$key}",
"\n";
}
?>
以上示例会输出:
never: sunrise never: sunset 12:52:18 AKST: transit 10:53:19 AKST: civil_twilight_begin 14:51:17 AKST: civil_twilight_end 09:01:47 AKST: nautical_twilight_begin 16:42:48 AKST: nautical_twilight_end 07:40:47 AKST: astronomical_twilight_begin 18:03:49 AKST: astronomical_twilight_end
示例 #3 Midnight sun (Tromsø, Norway)
<?php
$si = date_sun_info(strtotime("2022-06-26"), 69.68, 18.94);
print_r($si);
?>
以上示例会输出:
Array ( [sunrise] => 1 [sunset] => 1 [transit] => 1656240426 [civil_twilight_begin] => 1 [civil_twilight_end] => 1 [nautical_twilight_begin] => 1 [nautical_twilight_end] => 1 [astronomical_twilight_begin] => 1 [astronomical_twilight_end] => 1 )
示例 #4 Calculating length of day (Kyiv)
<?php
$si = date_sun_info(strtotime('2022-08-26'), 50.45, 30.52);
$diff = $si['sunset'] - $si['sunrise'];
echo "Length of day: ",
floor($diff / 3600), "h ",
floor(($diff % 3600) / 60), "s\n";
?>
以上示例会输出:
Length of day: 13h 56s