PHP 8.1: date_sunrise, date_sunset functions and related INI settings are deprecated


From PHP 8.1 and later, date_sunrise and date_sunset functions, along with their INI values, are deprecated in favor of date_sun_info function.

date_sunrise and date_sunset functions return sunrise and sunset times for a given day (passed as a UNIX timestamp) and a latitude/longitude pair. This function takes the default latitude and longitude values from PHP INI settings date.default_latitude and date.default_longitude. These INI settings are not used any other PHP functions.

Attempting to use date_sunrise or date_sunset functions emit a deprecation notice since PHP 8.1:

PHP Deprecated:  Function date_sunrise() is deprecated in ... on line ...
PHP Deprecated:  Function date_sunset() is deprecated in ... on line ...

Deprecated INI settings

In PHP 8.1, this change deprecates the following INI settings:

All four INI settings are only used by date_sunrise and date_sunset functions.

Setting these INI values either in a php.ini file or using ini_set functions do not emit a deprecation notice. However, they will be removed in PHP 9.0.

Deprecated constants

The following three PHP constants are only used by date_sunrise and date_sunset functions, and are deprecated as well. Using them does not emit a deprecation notice, but they will be removed in PHP 9.0.

  • SUNFUNCS_RET_STRING (assigned 1)
  • SUNFUNCS_RET_DOUBLE (assigned 2)

date_sun_info replacement

date_sunrise and date_sunset functions can be replaced with date_sun_info function calls. date_sun_info function is available in all current PHP versions, and return both sunrise and sunset times in the similar fashion as date_sunrise and date_sunset, and accepts an explicit longitude and latitude values in contrast to date_sunrise and date_sunset functions.

$time = time();
// Set latitude and longitude instead of setting them as INI settings.
$latitude = 52.391842;
$longitude = 13.063561;

- $sunrise = date_sunrise($time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
- $sunset  = date_sunset($time, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
+ $suninfo = date_sun_info($time, $latitude, $longitude);
+ $sunrise = $suninfo['sunrise'];
+ $sunset  = $suninfo['sunset'];

date_sunrise and date_sunset functions support returning the sunset/sunrise times as a string, timestamp or double value. Existing functions that relied on this behavior can replace them with date_sun_info function, followed by a manual date formatting:


date_sun_info function returns the sunrise/sunset timestamps as UNIX timestamps too, and does not require any date formatting to match date_sunrise and date_sunset outputs.


SUNFUNCS_RET_STRING parameter passed to date_sunrise and date_sunset functions return the sunrise/sunset times in the 'H:i' format accepted by date() function.

- $sunrise = date_sunrise($time, SUNFUNCS_RET_STRING, $latitude, $longitude);
+ $suninfo = date_sun_info($time, $latitude, $longitude);
+ $sunrise = date('H:i', $suninfo['sunrise']);


SUNFUNCS_RET_DOUBLE parameter returns the sunrise/sunset time as a float value. To match this output format, combine the hour ('H') format the fraction calculated from the seconds since the full hour past:

- $sunrise = date_sunrise($time, SUNFUNCS_RET_DOUBLE, $latitude, $longitude); // e.g. 18.098619869347
+ $suninfo = date_sun_info($time, $latitude, $longitude);
+ $sunrise = (int) date('H', $suninfo['sunrise']);
+ $sunrise += ($suninfo['sunrise'] % 3600) / 60 / 60; // e.g. 4.1561111111111

Backwards Compatibility Impact

Using date_sunrise and date_sunset functions in PHP 8.1 and later emits a deprecation notice. Using the INI settings and constants is also deprecated, but PHP does not emit deprecation notices on them.

From PHP 9.0, all these functions, INI settings and constants will be removed.

The date_sun_info replacement is compatible with older PHP versions as well, and provides a more intuitive approach to setting the latitude/longitude values.

RFC Implementation