English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP date_sunset() Function Usage and Example

PHP Date & Time Function Manual

The date_sunset() function returns the sunset time for the given date/The sunset time of the location.

Definition and Usage

date_sunset()The function accepts a timestamp representing a given day and returns the sunset time for that specific date.

Syntax

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

Parameter

Serial NumberParameters and Description
1

timestamp (required)

This specifies a timestamp.

2

format (optional)

This specifies the format you need to use for the result value. You can pass three constants as the value of this parameter;

That is: SUNFUNCS_RET_STRING(,, Integer.

3

latitude (optional)

By default, this option specifies the latitude of a location, which specifies the north direction. To specify the latitude value of the south, it must be passed as a negative value.

4

longitude (optional)

By default, it specifies the longitude of a location, which specifies the east direction. To specify the longitude value of the west, it must be passed as a negative value.

5

zenith (optional)

This specifies the zenith value. This specifies the angle between the line perpendicular to the Earth's surface and the center of the sun.

6

gmtoffset (optional)

This specifies the time difference between GMT and local time (in hours).

Return value

The date_sunset() function returns the sunset time in the required format. If it fails, it will return a boolean valuefalse.

PHP version

This function was originally introduced in PHP 5.0 version introduced and can be used in all higher versions.

Online example

The following example demonstratesdate_sunset()Function usage-

<?php
   $sun_info = date_sunset("02-17-2012");
   print_r($sun_info);
?>
Test and see‹/›

Output result

14:46

Online example

Now, to call this function, you must pass the latitude and longitude values. If you want to pass latitude and longitude values, you must also pass the required format values-

<?php
   $sun_info = date_sunset("02-03-2020", SUNFUNCS_RET_STRING, 23.4, -25);
   print_r("Sunset Time: ".$sun_info);
?>
Test and see‹/›

Output result

Sunset Time: 19:05

Online example

The following example verifies the case without sunset-

<?php
   $sun_info = date_sunset("25-12-2016", SUNFUNCS_RET_STRING, 69, 41);
   print("Sunset Time: ".$sun_info);
   print("\n");
   var_dump($sun_info);
?>
Test and see‹/›

Output result

Sunset Time:
bool(false)

Online example

<?php
   echo("Date: ".date("D M d Y"));
   echo("\n");
   echo("Sunset time: ");
   echo(date_sunset(time(), SUNFUNCS_RET_STRING,38.4,-9,90,1));
?>
Test and see‹/›

Output result

Date: Thu May 07 2020
Sunset time: 20:30