PHP date_sunset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

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.

PHP
$time = date_sunset(1718971200, SUNFUNCS_RET_STRING, 51.5074, -0.1278, 90.833333, 0);
CallWhat it does
date_sunset($ts, SUNFUNCS_RET_STRING, 51.5074, -0.1278, 90.833333, 0)Returns a time string like 20:21.
date_sunset($ts, SUNFUNCS_RET_TIMESTAMP, 51.5074, -0.1278, 90.833333, 0)Returns the sunset as a Unix timestamp (an int).
date_sunset($ts, SUNFUNCS_RET_DOUBLE, 51.5074, -0.1278, 90.833333, 0)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.

Library declarations
PHP
date_sunset(int $timestamp, int $returnFormat = SUNFUNCS_RET_STRING, ?float $latitude = null, ?float $longitude = null, ?float $zenith = null, ?float $utcOffset = null): string|int|float|false

Parameters

ParameterDescription
timestampRequired. A Unix timestamp that selects the day.
returnFormatOptional. SUNFUNCS_RET_STRING (default, text like 20:21), SUNFUNCS_RET_TIMESTAMP (int), or SUNFUNCS_RET_DOUBLE (float hours).
latitudeOptional. Degrees north. Defaults to the ini setting date.default_latitude.
longitudeOptional. Degrees east. Defaults to the ini setting date.default_longitude.
zenithOptional. The sun angle that counts as sunset. Use about 90.833333 for the standard definition.
utcOffsetOptional. The timezone offset in hours (for example 0 for UTC or 5.5 for India).

Return Value

Return ValueDescription
string|int|floatThe sunset time in the requested format.
falseReturned when there is no sunset on that day, such as in the Arctic summer.

PHP Examples

Example 1 — Sunset as a String (with Deprecation Notice)

On PHP 8.1 and later the function shows a deprecation message. This example catches it with an error handler so you can see it.

PHP
<?php
date_default_timezone_set("UTC");

set_error_handler(function ($severity, $message) {
    echo "Deprecated: ", $message, PHP_EOL;
    return true;
}, E_DEPRECATED);

$timestamp = 1718971200; // 2024-06-21 12:00:00 UTC
$sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, 51.5074, -0.1278, 90.833333, 0);
echo "London sunset (UTC): ", $sunset, PHP_EOL;

restore_error_handler();

How It Works

  1. set_error_handler() catches deprecation messages so we can print them ourselves.
  2. date_sunset() is called with the London coordinates, a standard zenith, and a UTC offset of 0.
  3. On PHP 8. 1 and later the handler prints the deprecation notice. On older versions that first line does not appear.
  4. The string result 20:21 is the approximate sunset time in UTC.

Example 2 — Return Formats and No Sunset

Compare the return formats. In the Arctic summer the sun does not set, so the result is false.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED); // hide the 8.1+ deprecation message
date_default_timezone_set("UTC");

$timestamp = 1718971200;
$asInt = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, 51.5074, -0.1278, 90.833333, 0);
$asFloat = date_sunset($timestamp, SUNFUNCS_RET_DOUBLE, 51.5074, -0.1278, 90.833333, 0);
$polar = date_sunset($timestamp, SUNFUNCS_RET_STRING, 69.6492, 18.9553, 90.833333, 0);

echo gettype($asInt), PHP_EOL;
echo gettype($asFloat), " ", floor($asFloat), PHP_EOL;
echo "Polar: ", var_export($polar, true), PHP_EOL;

How It Works

  1. error_reporting() hides the deprecation message so only our output is shown.
  2. SUNFUNCS_RET_TIMESTAMP returns an integer timestamp.
  3. SUNFUNCS_RET_DOUBLE returns hours after midnight, so about 20. 35 is shown as 20 after floor().
  4. In Tromso on June 21 the sun never sets, so PHP returns false.

Example 3 — Is It Day or Night? Use date_sun_info()

The modern replacement gives you sunrise and sunset together, which makes day and night checks easy.

PHP
<?php
date_default_timezone_set("UTC");

$info = date_sun_info(1718971200, 51.5074, -0.1278);

foreach ([1718971200, 1719007200] as $now) {
    $isDay = $now >= $info["sunrise"] && $now < $info["sunset"];
    echo date("H:i", $now), " UTC: ", $isDay ? "Daytime" : "Night", PHP_EOL;
}

How It Works

  1. date_sun_info() returns sunrise (about 03:43) and sunset (about 20:21) for London.
  2. The loop checks two moments: noon and 22:00 UTC.
  3. A moment is daytime when it falls on or after sunrise and before sunset.
  4. 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.

Next: date_time_set()

date_time_set() changes the hour, minute, second, and microsecond of a DateTime object whi.

date_time_set() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful