In PHP, date_sunset() returns the time of sunset for a given day and location, but it is deprecated as of PHP 8.1.
It works like date_sunrise(), and you can ask for 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_sunset() with the values you need. Start with this line.
Returns the sunset as hours after midnight (a float).
DEPRECATED as of PHP 8.1: calling date_sunset() 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() returns sunrise (about 03:43) and sunset (about 20:21) for London.
The loop checks two moments: noon and 22:00 UTC.
A moment is daytime when it falls on or after sunrise and before sunset.
Noon is inside that window, while 22:00 is after sunset, so it is night.
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 each value is a timestamp you can format with date().
They choose the return type: SUNFUNCS_RET_STRING for text like 20:21, SUNFUNCS_RET_TIMESTAMP for a Unix timestamp, and SUNFUNCS_RET_DOUBLE for hours as a float.
It returns false when the sun does not set on that day at that location, such as during polar summer or polar night.
It is best to pass it. The offset in hours decides which timezone the result is shown in. Using 0 gives UTC, and 5.5 gives India Standard Time.