PHP date_sunset() Function

Example

Return the sunset time for Lisbon, Portugal today:

<?php
// Shanghai, China:
// Latitude: 31.22 North, Longitude: 121.47 West
// Zenith ~= 90, Offset: +8 GMT
echo("Shanghai, China: Date: " . date("D M d Y"));
echo("<br>Time of sunrise:");
echo(date_sunrise(time(), SUNFUNCS_RET_STRING, 31.22, 121.47, 90, 8));
?>

Run Example

Definition and Usage

The date_sunset() function returns the sunset time for a specified date and location.

Tip:See also date_sunrise() Function, returns the sunrise time for a specified date and location.

Syntax

date_sunset(timestamp,format,latitude,longitude,zenith,gmtoffset);
Parameters Description
timestamp Required. Specifies the timestamp of the date and time to calculate the sunset time.
format

Optional. Specifies how to return the result:

  • SUNFUNCS_RET_STRING (Returns the result as a string, such as 16:46) (default)
  • SUNFUNCS_RET_DOUBLE (Returns the result as a floating-point number, such as 16.78243132)
  • SUNFUNCS_RET_TIMESTAMP (Returns the result as an integer (timestamp), such as 1095034606)
latitude Optional. Specifies the latitude of the location. The default is North latitude. To specify South latitude, please pass a negative value.
longitude Optional. Specifies the longitude of the location. The default is East longitude. To specify West longitude, please pass a negative value.
zenith Optional. The default is date.sunset_zenith.
gmtoffset Optional. Specifies the difference in hours between GMT and local time.

Technical Details

Return Value: If successful, it returns the sunset time in the specified format. If it fails, it returns FALSE.
PHP Version: 5+
Update Log: Starting from PHP 5.1.0, the function reports E_STRICT and E_NOTICE time zone errors when it occurs.