In PHP, date_sunrise() returns the time of sunrise for a given day and location, but it is deprecated as of PHP 8.1.
You can ask for the result as a string, a Unix timestamp, or a number of hours. New code should use date_sun_info(), which returns sunrise, sunset, and twilight times in one array.
Syntax
Call date_sunrise() with the values you need. Start with this line.
Returns the sunrise as hours after midnight (a float).
DEPRECATED as of PHP 8.1: calling date_sunrise() triggers a deprecation message, and it may be removed in a future version. Use date_sun_info() instead. The examples below hide or capture that message so the output stays clean. The time shown is approximate.
date_sun_info() takes the timestamp, latitude, and longitude. There is no zenith or offset to remember.
It returns an array, so one call covers both sunrise and sunset.
date("H:i") formats each timestamp using the default timezone.
The sunrise matches what date_sunrise() gave in the first example, without the deprecation message.
Frequently Asked Questions
Yes. It is deprecated as of PHP 8.1 and shows a deprecation message when called. Use date_sun_info() in new code.
Use date_sun_info(). It returns the sunrise, sunset, solar noon, and twilight times in one array, and the sunrise value is a timestamp you can format with date().
SUNFUNCS_RET_STRING returns text like 03:43, SUNFUNCS_RET_TIMESTAMP returns a Unix timestamp, and SUNFUNCS_RET_DOUBLE returns hours as a float, such as 3.72.
It returns false when the sun does not rise on that day at that location, such as during polar summer or polar night.
Use about 90.833333, which accounts for the size of the sun and the bending of light by the atmosphere. Other values give civil or nautical twilight instead.