PHP Function Reference

PHP date_sunset() Function



The PHP date_sunset() function returns the sunset time for a given day (specified as a timestamp) and location.

Syntax

date_sunset(timestamp, returnFormat, 
            latitude, longitude, 
            zenith, utcOffset)

Parameters

timestamp Required. Specify the timestamp of the day from which the sunset time is taken.
returnFormat Optional. Specify how to return the result. Default is SUNFUNCS_RET_STRING. The possible values are:
  • SUNFUNCS_RET_STRING - returns the result as string. e.g. 16:46
  • SUNFUNCS_RET_DOUBLE - returns the result as float. e.g. 16.78243132
  • SUNFUNCS_RET_TIMESTAMP - returns the result as integer (timestamp). e.g. 1095034606
latitude Optional. Specify the latitude of the location. Default is North. For South, pass in a negative value. See also date.default_latitude.
longitude Optional. Specify the longitude of the location. Default is East. For West, pass in a negative value. See also date.default_longitude.
zenith Optional. It is the angle between the center of the sun and a line perpendicular to earth's surface. Defaults is date.sunset_zenith. Some common zenith angles are:
  • 90°50' - Sunrise: the point where the sun becomes visible.
  • 96° - Civil twilight: conventionally used to signify the start of dawn.
  • 102° - Nautical twilight: the point at which the horizon starts being visible at sea.
  • 108° - Astronomical twilight: the point at which the sun starts being the source of any illumination.
utcOffset Optional. Specify the difference between GMT and local time in hours. It is ignored, if returnFormat is SUNFUNCS_RET_TIMESTAMP.

Return Value

Returns the sunset time in a specified returnFormat on success or false on failure.

Exceptions

If the timezone is not valid, the function will generate a E_WARNING.

Example:

The example below shows the usage of date_sunset() function.

<?php
//London, UK - Latitude: 51.51 North, Longitude: 0.13 West
//Zenith ~= 90, offset: (winter +0 GMT, summer +1 GMT)

//creating timestamp for some summer and winter days
$summer = mktime(0,0,0,06,30,2016);  //30 Jun 2016
$winter = mktime(0,0,0,12,31,2016);  //31 Dec 2016

echo("London, UK\nDate: ".date("d-M-Y", $summer));
echo(", Sunset time: ");
echo date_sunset($summer,SUNFUNCS_RET_STRING,51.51,-0.13,90,1);

echo("\nDate: ".date("d-M-Y", $winter));
echo(", Sunset time: ");
echo date_sunset($winter,SUNFUNCS_RET_STRING,51.51,-0.13,90,0);
?>

The output of the above code will be:

London, UK
Date: 30-Jun-2016, Sunset time: 21:16
Date: 31-Dec-2016, Sunset time: 15:56

Example:

Consider the example below which can be used to get the sunrise and sunset time of the current date.

<?php
//Lisbon, Portugal - Latitude: 38.4 North, Longitude: 9 West
//Zenith ~= 90, offset: +1 GMT

echo("Lisbon, Portugal\nDate: ".date("d-M-Y"));
echo("\nSunrise time: ");
echo date_sunrise(time(),SUNFUNCS_RET_STRING,38.4,9,90,1);
echo("\nSunset time:  ");
echo date_sunset(time(),SUNFUNCS_RET_STRING,38.4,9,90,1);
?>

The output of the above code will be:

Lisbon, Portugal
Date: 11-Sep-2021
Sunrise time: 06:05
Sunset time:  18:35

❮ PHP Date and Time Reference