date_sun_info()

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

Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.1.2, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

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

Parameters

ParameterTypeWhat it takes
$timestampintUnix timestamp.
$latitudefloatLatitude in degrees.
$longitudefloatLongitude in degrees.

Return value

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. ...

Example

A runnable snippet:

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"; }

Output:

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

Changelog

VersionChange
7.2.0The calculation was fixed with regards to local midnight instead of local noon, which changes the results slightly.

All PHP 8.5 built-in functions

Reference card built from the PHP manual page date_sun_info() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.