Thursday, October 23, 2025
HomeLanguagesPHP | date_sunset() Function

PHP | date_sunset() Function

The date_sunset() is an inbuilt function in PHP which is used to find the sunset time for a specified day and location.

Syntax:

date_sunset ( $timestamp, $format, $latitude, $longitude, $zenith, $gmtoffset )

Parameters: This function accepts four parameters as mentioned above and described below.

  • $timestamp: It is a required parameter which specifies the timestamp of the day from which the sunset time is taken.
  • $format: It is an optional parameter which specifies the format to return the result. Return format are given below:
    • SUNFUNCS_RET_STRING: Returns a string. e.g. 16:46 (by default)
    • SUNFUNCS_RET_DOUBLE: Returns a float. e.g. 16.12345
    • SUNFUNCS_RET_TIMESTAMP: Returns the result as integer (timestamp). e.g. 987123569
  • $latitude: It is an optional parameter which specifies the latitude of the location. By default, it set as North. To specify a value for South, pass in a negative value.
  • $longitude: It is an optional parameter which specifies the longitude of the location. By default, it set as East. To modify a value for West, pass in a negative value.
  • $zenith: It is an Optional parameter. The zenith is the angle between the center of the sun and a line perpendicular to earth’s surface, by default it is date.sunset_zenith.
  • $gmtoffset: It is optional parameter and used to specifies the difference between GMT and local time in hours.

Return Value: It returns the time of the sunset, in the specified format, on success. FALSE on failure.

Exceptions: This function generates E_NOTICE error if date/time function is invalid and E_STRICT or E_WARNING if using the system setting or the TZ environment variable.

Below programs illustrate the date_sunset() function in PHP.

Program 1:




<?php
// PHP program to show sunset time 
// of New delhi india for current day
  
// Longitude and latitude of Delhi India
// 28.6139° N, 77.2090° E
// GMT(Greenwich Mean Time) +5.30
// Zenith ~= 90
  
echo date("D M d Y");
echo("\nSunset time: ");
echo(date_sunset(time(), SUNFUNCS_RET_STRING,
                 28.6139, 77.2090, 90, 5.30));
?>


Output:

Wed Jun 27 2018
Sunset time: 19:07

Program 2:




<?php
// PHP program to show sunset time 
// of GFG Noida for a Current day
  
// Longitude and latitude of neveropen
// Noida 28°30'04.0"N 77°24'36.0"E
// GMT(Greenwich Mean Time) +5.30
// Zenith ~= 90
  
echo date("D M d Y");
echo("\nSunset time: ");
echo(date_sunset(time(), SUNFUNCS_RET_STRING,
              28.501120, 77.409989, 90, 5.30));
?>


Output:

Wed Jun 27 2018
Sunset time: 19:06

Related Articles:

Reference: http://php.net/manual/en/function.date-sunset.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS