date_sunset()
Learn how date_sunset() worked in PHP 7.x, why it was removed in PHP 8.1, and how to get sunset times in modern PHP.
Introduction
date_sunset() is a legacy PHP function that returns the time of sunset for a given date and geographic location. This page explains what it returned, how its parameters worked, why it was removed from modern PHP, and what to use in its place. If you are writing new code, jump to Migration to modern PHP — but understanding the original signature is still useful when maintaining older codebases.
date_sunset() was deprecated in PHP 8.0 and removed in PHP 8.1. It will throw an Error ("Call to undefined function") on any current PHP runtime. Use it only when maintaining legacy PHP 7.x code; for anything new, use a library or an external API instead.
What date_sunset() does
date_sunset() calculated the moment the sun dipped below the horizon for a specific date and a specific point on Earth. It needed four pieces of information:
- A date, supplied as a Unix timestamp.
- A latitude and longitude identifying the observer's position.
- A zenith angle — the angle, measured down from straight overhead, at which the sun is considered "set." The default
90.83°is slightly more than a right angle because it compensates for atmospheric refraction (which bends sunlight and makes the sun appear higher than it really is) and for the apparent radius of the sun's disc.
By default the function returned a string like "18:12", but with the right flag it could return the result as a Unix timestamp, which you can then format with date() or convert with any other date function.
Syntax
date_sunset(
int $timestamp,
int $returnFormat = SUNFUNCS_RET_STRING,
float $latitude = ini_get("date.default_latitude"),
float $longitude = ini_get("date.default_longitude"),
float $zenith = ini_get("date.sunset_zenith"),
float $utcOffset = 0
): mixedParameters
- timestamp — The Unix timestamp of the day to calculate sunset for. Build it with
strtotime()ormktime(). - returnFormat — One of three constants that control the return type:
SUNFUNCS_RET_STRING— a"HH:MM"string (the default).SUNFUNCS_RET_DOUBLE— the time as hours since midnight (a float, e.g.18.2).SUNFUNCS_RET_TIMESTAMP— a Unix timestamp.
- latitude — The observer's latitude in degrees (positive = north).
- longitude — The observer's longitude in degrees (positive = east, negative = west).
- zenith — The sun's zenith angle at sunset; defaults to
90.83°. - utcOffset — The offset from UTC in hours; ignored when
returnFormatis a timestamp.
The function returned false on locations and dates where the sun never sets or never rises (for example, the poles in summer or winter).
Example (legacy PHP 7.x)
The following runs on PHP 7.x or earlier, where the function still exists:
Output:
Sunset on March 3, 2023 in San Francisco was at 18:12Because the result is a timestamp, you can reformat it any way you like — date("g:i A", $sunset) would print 6:12 PM instead.
Migration to modern PHP
date_sunset() no longer exists, and PHP's built-in DateTime class handles time zones and formatting but does not compute sun positions. So a modern app has two practical options.
Option 1 — Call a sunrise/sunset API
The simplest portable approach is to query a public service and read the timestamp it returns. This works on any PHP version and needs no math on your side:
<?php
// Free, no-key API: https://sunrise-sunset.org/api
$lat = 37.7749;
$lng = -122.4194;
$date = '2023-03-03';
$url = "https://api.sunrise-sunset.org/json?lat={$lat}&lng={$lng}&date={$date}&formatted=0";
$response = json_decode(file_get_contents($url), true);
// The API returns ISO-8601 UTC strings
$sunsetUtc = new DateTime($response['results']['sunset']);
$sunsetUtc->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo "Sunset: " . $sunsetUtc->format('H:i');
?>Option 2 — Use an astronomy library
For an offline, self-contained calculation, install a maintained Composer package that ports the same sunset algorithm (search Packagist for "sunrise sunset"; popular choices include tienvx/php-sunrise-sunset and andreas-glaser/php-sun-info). The exact class names depend on the package, so always check its README — the general shape is: construct a calculator from a latitude/longitude, then ask it for the sunset of a given date.
Where sunset times are used
Whether you call date_sunset() (legacy) or one of the modern alternatives above, sunset data shows up in many kinds of apps:
- Weather apps — display the day's sunset alongside the forecast for a location.
- Photography apps — derive the "golden hour," the period just before sunset when light is soft and warm, by subtracting an hour from the sunset time.
- Event and scheduling apps — flag outdoor events that run until dusk, or switch a site to dark styling after dark.
- Smart-home and lighting — trigger lights or routines at the local sunset time.
Pair the sunset time with date_sunrise() to get the full daylight window for a location.
Conclusion
date_sunset() was a convenient one-call way to get sunset times in PHP 7.x, but it was removed in PHP 8.1 and will fail on any modern runtime. For new code, call a sunrise/sunset API or use a maintained astronomy library, and format the result with the DateTime class. The use cases — weather, photography, events, automation — are unchanged; only the calculation source has moved out of PHP core.