date_sun_info

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

返回指定坐标与日期的日出/日落/中天及时辰边界时间戳数组(天文确定值);替代 8.1 已弃用的 date_sunrise/date_sunset;极圈边界以 bool 填充。

C C23

C23
#include <stdio.h>
#include <time.h>

int main(void) {
    /* 这三个时间戳由 PHP date_sun_info 实测得出;C 负责格式化同一份数据。
       固定坐标(北京 39.9N 116.4E)2026-06-21。 */
    long sunrise = 1781988356, sunset = 1782042379, transit = 1782015367;
    char buf[32];
    struct tm tm;
    time_t t = (time_t)sunrise;
    gmtime_r(&t, &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
    printf("sunrise %s UTC\n", buf);
    t = (time_t)sunset;
    gmtime_r(&t, &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
    printf("sunset  %s UTC\n", buf);
    t = (time_t)transit;
    gmtime_r(&t, &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
    printf("transit %s UTC\n", buf);
    long day = sunset - sunrise;
    printf("day length: %ld s = %ld h %ld m\n", day, day / 3600, day % 3600 / 60);
    printf("ordered: %s\n", sunrise < transit && transit < sunset ? "true" : "false");
    return 0;
}
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: true

C++ C++26

C++26
#include <cstdio>
#include <ctime>

int main() {
    // 这三个时间戳由 PHP date_sun_info 实测得出;C++ 负责格式化同一份数据。
    // 固定坐标(北京 39.9N 116.4E)2026-06-21。
    long sunrise = 1781988356, sunset = 1782042379, transit = 1782015367;
    char buf[32];
    struct tm tm;
    time_t t = static_cast<time_t>(sunrise);
    gmtime_r(&t, &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
    std::printf("sunrise %s UTC\n", buf);
    t = static_cast<time_t>(sunset);
    gmtime_r(&t, &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
    std::printf("sunset  %s UTC\n", buf);
    t = static_cast<time_t>(transit);
    gmtime_r(&t, &tm);
    strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
    std::printf("transit %s UTC\n", buf);
    long day = sunset - sunrise;
    std::printf("day length: %ld s = %ld h %ld m\n", day, day / 3600, day % 3600 / 60);
    std::printf("ordered: %s\n", sunrise < transit && transit < sunset ? "true" : "false");
    return 0;
}
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: true

C# v11.0.100

v11.0.100
// 这三个时间戳由 PHP date_sun_info 实测得出;C# 负责格式化同一份数据。
// 固定坐标(北京 39.9N 116.4E)2026-06-21。
long sunrise = 1781988356L, sunset = 1782042379L, transit = 1782015367L;
var inv = System.Globalization.CultureInfo.InvariantCulture;
Console.WriteLine("sunrise " + DateTimeOffset.FromUnixTimeSeconds(sunrise).UtcDateTime.ToString("yyyy-MM-dd HH:mm", inv) + " UTC");
Console.WriteLine("sunset  " + DateTimeOffset.FromUnixTimeSeconds(sunset).UtcDateTime.ToString("yyyy-MM-dd HH:mm", inv) + " UTC");
Console.WriteLine("transit " + DateTimeOffset.FromUnixTimeSeconds(transit).UtcDateTime.ToString("yyyy-MM-dd HH:mm", inv) + " UTC");
long day = sunset - sunrise;
Console.WriteLine("day length: " + day + " s = " + day / 3600 + " h " + day % 3600 / 60 + " m");
Console.WriteLine("ordered: " + (sunrise < transit && transit < sunset));
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"time"
)

func main() {
	// 这三个时间戳由 PHP date_sun_info 实测得出;Go 负责格式化同一份数据。
	// 固定坐标(北京 39.9N 116.4E)2026-06-21。
	sunrise, sunset, transit := int64(1781988356), int64(1782042379), int64(1782015367)
	fmt.Println("sunrise", time.Unix(sunrise, 0).UTC().Format("2006-01-02 15:04"), "UTC")
	fmt.Println("sunset ", time.Unix(sunset, 0).UTC().Format("2006-01-02 15:04"), "UTC")
	fmt.Println("transit", time.Unix(transit, 0).UTC().Format("2006-01-02 15:04"), "UTC")
	day := sunset - sunrise
	fmt.Printf("day length: %d s = %d h %d m\n", day, day/3600, day%3600/60)
	fmt.Println("ordered:", sunrise < transit && transit < sunset)
}
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: true

Java v27.0.1

v27.0.1
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        // 这三个时间戳由 PHP date_sun_info 实测得出;Java 负责格式化同一份数据。
        // 固定坐标(北京 39.9N 116.4E)2026-06-21。
        long sunrise = 1781988356L, sunset = 1782042379L, transit = 1782015367L;
        System.out.println("sunrise " + Instant.ofEpochSecond(sunrise));
        System.out.println("sunset  " + Instant.ofEpochSecond(sunset));
        System.out.println("transit " + Instant.ofEpochSecond(transit));
        long day = sunset - sunrise;
        System.out.printf("day length: %d s = %d h %d m%n", day, day / 3600, day % 3600 / 60);
        System.out.println("ordered: " + (sunrise < transit && transit < sunset));
    }
}
输出
sunrise 2026-06-20T20:45:56Z
sunset  2026-06-21T11:46:19Z
transit 2026-06-21T04:16:07Z
day length: 54023 s = 15 h 0 m
ordered: true

JavaScript v26.13.1

v26.13.1
// 这三个时间戳由 PHP date_sun_info 实测得出;JS 负责格式化同一份数据。
// 固定坐标(北京 39.9N 116.4E)2026-06-21。
const sunrise = 1781988356, sunset = 1782042379, transit = 1782015367;
const fmt = (ts) => new Date(ts * 1000).toISOString().slice(0, 16).replace("T", " ") + " UTC";
console.log("sunrise", fmt(sunrise));
console.log("sunset ", fmt(sunset));
console.log("transit", fmt(transit));
const day = sunset - sunrise;
console.log(`day length: ${day} s = ${Math.floor(day / 3600)} h ${Math.floor((day % 3600) / 60)} m`);
console.log("ordered:", sunrise < transit && transit < sunset);
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: true

PHP v8.6.0

v8.6.0
// date_sun_info:固定坐标与日期的日出/日落/中天时刻
date_default_timezone_set('UTC'); // 格式化确定性
$ts = mktime(12, 0, 0, 6, 21, 2026); // 2026-06-21 当天任一时刻
$info = date_sun_info($ts, 39.9042, 116.4074); // 北京 39.9N 116.4E
printf("sunrise %s UTC\n", date('Y-m-d H:i', $info['sunrise']));
printf("sunset  %s UTC\n", date('Y-m-d H:i', $info['sunset']));
printf("transit %s UTC\n", date('Y-m-d H:i', $info['transit']));
$day = $info['sunset'] - $info['sunrise'];
printf("day length: %d s = %d h %d m\n", $day, intdiv($day, 3600), intdiv($day % 3600, 60));
var_dump($info['sunrise'] < $info['transit'] && $info['transit'] < $info['sunset']);
// 极圈边界:夏至的北极圈内 "sunrise" 返回 true(太阳永不落)
var_dump(date_sun_info(mktime(12, 0, 0, 6, 21, 2026), 78.2, 15.6)['sunrise']);
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
bool(true)
bool(true)

Python v3.15.2

v3.15.2
from datetime import datetime, timezone

# 这三个时间戳由 PHP date_sun_info 实测得出;其他语言格式化同一份数据。
# 固定坐标(北京 39.9N 116.4E)2026-06-21——天文确定值。
sunrise, sunset, transit = 1781988356, 1782042379, 1782015367
utc = timezone.utc
print("sunrise", datetime.fromtimestamp(sunrise, tz=utc).strftime("%Y-%m-%d %H:%M"), "UTC")
print("sunset ", datetime.fromtimestamp(sunset, tz=utc).strftime("%Y-%m-%d %H:%M"), "UTC")
print("transit", datetime.fromtimestamp(transit, tz=utc).strftime("%Y-%m-%d %H:%M"), "UTC")
day = sunset - sunrise
print(f"day length: {day} s = {day // 3600} h {day % 3600 // 60} m")
print("ordered:", sunrise < transit < sunset)
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: True

Rust v1.99.1

v1.99.1
use chrono::DateTime;

fn main() {
    // 这三个时间戳由 PHP date_sun_info 实测得出;Rust 负责格式化同一份数据。
    // 固定坐标(北京 39.9N 116.4E)2026-06-21。
    let sunrise = 1781988356i64;
    let sunset = 1782042379i64;
    let transit = 1782015367i64;
    let fmt = |ts: i64| {
        DateTime::from_timestamp(ts, 0)
            .unwrap()
            .format("%Y-%m-%d %H:%M UTC")
            .to_string()
    };
    println!("sunrise {}", fmt(sunrise));
    println!("sunset  {}", fmt(sunset));
    println!("transit {}", fmt(transit));
    let day = sunset - sunrise;
    println!("day length: {} s = {} h {} m", day, day / 3600, day % 3600 / 60);
    println!("ordered: {}", sunrise < transit && transit < sunset);
}
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: true

TypeScript v26.13.1

v26.13.1
// 这三个时间戳由 PHP date_sun_info 实测得出;TS 负责格式化同一份数据。
// 固定坐标(北京 39.9N 116.4E)2026-06-21。
const sunrise = 1781988356, sunset = 1782042379, transit = 1782015367;
const fmt = (ts: number): string => new Date(ts * 1000).toISOString().slice(0, 16).replace("T", " ") + " UTC";
console.log("sunrise", fmt(sunrise));
console.log("sunset ", fmt(sunset));
console.log("transit", fmt(transit));
const day = sunset - sunrise;
console.log(`day length: ${day} s = ${Math.floor(day / 3600)} h ${Math.floor((day % 3600) / 60)} m`);
console.log("ordered:", sunrise < transit && transit < sunset);
输出
sunrise 2026-06-20 20:45 UTC
sunset  2026-06-21 11:46 UTC
transit 2026-06-21 04:16 UTC
day length: 54023 s = 15 h 0 m
ordered: true

语言差异说明

C:标准库无天文计算——NOAA/SOFA 公式手写或第三方库;示例格式化 PHP 实测的固定时间戳(同一份数据跨语言验证格式化)。

C++:同 C——无标准天文 API;实战可用 libnova/boost 无关的自研公式。

C#:无内置天文 API——惯用第三方库或 NOAA 公式手写;示例格式化固定时间戳。

Go:标准库无天文 API——惯用第三方(如 github.com/soniakeys/meeus);示例格式化固定时间戳。

Java:标准库无天文 API——惯用第三方(如 sheries/SunPosition 自研);示例用 Instant 格式化固定时间戳。

JavaScript:无内置天文 API——惯用 suncalc(经典轻量库,精度有限);示例格式化固定时间戳。

PHP:date_sun_info 一次拿到 sunrise/sunset/transit 与三级晨昏界;时间戳为 UTC 时刻(格式化时注意时区);极圈边界以 bool(true)(永昼/永夜)填充而非时间戳;8.1 起弃用的 date_sunrise/date_sunset(弃用清单原文:"Use date_sun_info()")被它收口——旧函数每次只能取一个时刻,新函数一次全量;算法有文档注明的假定与精度边界(高纬度误差增大)。

Python:标准库无天文 API——惯用第三方 astral(坐标+日期一次算全,含晨昏);示例格式化固定时间戳。

Rust:无内置天文 API——惯用第三方(如 sunrise crate);示例用 chrono 格式化固定时间戳。

TypeScript:同 JavaScript——suncalc 或 suncalc3。