date_sun_info
date_sun_info(int $timestamp, float $latitude, float $longitude): array
Returns sunrise/sunset/transit and twilight boundaries as a timestamp array for given coordinates and date (astronomically fixed); it replaces the 8.1-deprecated date_sunrise/date_sunset; polar edges fill with bool.
C C23
#include <stdio.h>
#include <time.h>
int main(void) {
/* PHP date_sun_info measured these; C formats the SAME data.
Fixed coordinates (Beijing 39.9N 116.4E) on 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
#include <cstdio>
#include <ctime>
int main() {
// PHP date_sun_info measured these; C++ formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
// PHP date_sun_info measured these; C# formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
package main
import (
"fmt"
"time"
)
func main() {
// PHP date_sun_info measured these; Go formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
import java.time.Instant;
public class Main {
public static void main(String[] args) {
// PHP date_sun_info measured these; Java formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
// PHP date_sun_info measured these; JS formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
// date_sun_info: sunrise/sunset/transit for a fixed place and date
date_default_timezone_set('UTC'); // deterministic formatting
$ts = mktime(12, 0, 0, 6, 21, 2026); // any moment on 2026-06-21
$info = date_sun_info($ts, 39.9042, 116.4074); // Beijing 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']);
// Polar edge: inside the Arctic circle on midsummer day, "sunrise" is true (sun never sets)
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
from datetime import datetime, timezone
# PHP date_sun_info measures these; other languages format the SAME data.
# Fixed coordinates (Beijing 39.9N 116.4E) on 2026-06-21 - astronomically fixed.
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
use chrono::DateTime;
fn main() {
// PHP date_sun_info measured these; Rust formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
// PHP date_sun_info measured these; TS formats the SAME data.
// Fixed coordinates (Beijing 39.9N 116.4E) on 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
Language differences
C: no astronomy in the standard library - hand-write NOAA/SOFA formulas or use a third party; the example formats the fixed timestamps PHP measured (one dataset verified across languages).
C++: same as C - no standard astronomy API; real work uses libnova or hand-rolled formulas.
C#: no built-in astronomy API - third-party libraries or hand-written NOAA formulas; the example formats the fixed timestamps.
Go: no astronomy in the standard library - third parties like github.com/soniakeys/meeus; the example formats the fixed timestamps.
Java: no astronomy in the standard library - third parties or hand-rolled SunPosition; the example formats the fixed timestamps with Instant.
JavaScript: no built-in astronomy API - suncalc is the classic lightweight pick (limited precision); the example formats the fixed timestamps.
PHP: date_sun_info fetches sunrise/sunset/transit and three twilight tiers in one call; the timestamps are UTC instants (mind the time zone when formatting); polar edges fill with bool(true) (polar day/night) instead of timestamps; the 8.1-deprecated date_sunrise/date_sunset (deprecation note text: "Use date_sun_info()") were consolidated by it - the old pair returned one moment per call, the new one returns everything at once; the algorithm has documented assumptions and precision limits (errors grow at high latitudes).
Python: no astronomy in the standard library - astral is the usual third party (coordinates + date computed in one call, twilight included); the example formats the fixed timestamps.
Rust: no built-in astronomy API - the sunrise crate is a common pick; the example formats the fixed timestamps with chrono.
TypeScript: same as JavaScript - suncalc or suncalc3.