大网数据广告活动
PHP 语言与运行手册

date_sun_info · Returns an array with information about sunset/sunrise and twilight begin/end

· 52CE 我爱测

date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end

date_sun_info

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

date_sun_info — Returns an array with information about sunset/sunrise and twilight begin/end

说明

function date_sun_info(int $timestamp, float $latitude, float $longitude): array

参数

timestamp

Unix timestamp.

latitude

Latitude in degrees.

longitude

Longitude in degrees.

返回值

Returns an array whose structure is detailed in the following list:

sunrise

The timestamp of the sunrise (zenith angle = 90°35').

sunset

The timestamp of the sunset (zenith angle = 90°35').

transit

The timestamp when the sun is at its zenith, i.e. has reached its topmost point.

civil_twilight_begin

The start of the civil dawn (zenith angle = 96°). It ends at sunrise.

civil_twilight_end

The end of the civil dusk (zenith angle = 96°). It starts at sunset.

nautical_twilight_begin

The start of the nautical dawn (zenith angle = 102°). It ends at civil_twilight_begin.

nautical_twilight_end

The end of the nautical dusk (zenith angle = 102°). It starts at civil_twilight_end.

astronomical_twilight_begin

The start of the astronomical dawn (zenith angle = 108°). It ends at nautical_twilight_begin.

astronomical_twilight_end

The end of the astronomical dusk (zenith angle = 108°). It starts at 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
date_default_timezone_set('Asia/Jerusalem');
$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: 06:29:21
sunset: 16:36:00
transit: 11:32:41
civil_twilight_begin: 06:02:36
civil_twilight_end: 17:02:45
nautical_twilight_begin: 05:32:14
nautical_twilight_end: 17:33:08
astronomical_twilight_begin: 05:02:31
astronomical_twilight_end: 18:02:51

示例 #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), "m\n";

以上示例会输出:

Length of day: 13h 53m

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。