In PHP, date_sun_info() returns an array with the sunrise, sunset, solar noon, and twilight times for a given day and place.
You give it a timestamp and the latitude and longitude of a location. It is the modern replacement for the deprecated date_sunrise() and date_sunset() functions.
Syntax
Call date_sun_info() with the values you need. Start with this line.
Returns sun times for London on the day of the timestamp.
$info["sunrise"]
A timestamp for sunrise, or true or false when the sun never sets or never rises.
$info["transit"]
A timestamp for solar noon, when the sun is highest.
Most values are Unix timestamps, so format them with date() after setting your timezone. The times can differ from other sun calculators by about a minute. In polar regions, the values can be true (the sun stays up) or false (the sun stays down) instead of a timestamp.
Required. A Unix timestamp. PHP uses the day this timestamp falls on.
latitude
Required. Degrees north of the equator. Use a negative number for the south.
longitude
Required. Degrees east of Greenwich. Use a negative number for the west.
Return Value
Return Value
Description
array
An array with the keys sunrise, sunset, transit, civil_twilight_begin, civil_twilight_end, nautical_twilight_begin, nautical_twilight_end, astronomical_twilight_begin, and astronomical_twilight_end.
PHP Examples
Example 1 — See the Array Keys
Every call returns the same nine keys for any location. Print them to see what you can read.
The sunrise value is a timestamp, so date("H:i") turns it into hours and minutes.
Solar noon is a little after 12:00 UTC because London is slightly west of Greenwich.
All times are in UTC. In summer London clocks are one hour ahead, so the local sunrise is about 04:43.
Frequently Asked Questions
It returns an array with timestamps for sunrise, sunset, solar noon (transit), and the start and end of civil, nautical, and astronomical twilight for a place and day.
Use date_sun_info(). The functions date_sunrise() and date_sunset() are deprecated as of PHP 8.1, and date_sun_info() gives you all the sun times in one call.
In polar regions the sun may stay above the horizon all day (true) or below it all day (false). Twilight values can also be false in places where it never gets that dark.
Set the timezone with date_default_timezone_set() and then use date("H:i", $info["sunrise"]). The timestamp is an exact moment, so date() converts it for you.
No. They are calculated, so they can be off by a minute or so compared with other sun calculators. They are good enough for most websites and apps.