Current Location: Home> Latest Articles> How to get the sunset time of the current date through PHP's date_sunset function?

How to get the sunset time of the current date through PHP's date_sunset function?

M66 2025-05-31

In PHP, we can use the date_sunset function to get the sunset time at a specified location. This function is very convenient. It can calculate the sunset time of the current date or the specified date based on a given latitude and longitude. Next, we will introduce in detail how to use the date_sunset function to get the sunset time of the current date and do some necessary configuration and explanation.

1. Introduction to date_sunset function

The date_sunset function is used to return the sunset time of a certain date and place. Its syntax is as follows:

 date_sunset(time $timestamp, int $format = SUNFUNCS_RET_STRING, float $latitude = NULL, float $longitude = NULL, float $zenith = 90.5, float $gmt_offset = 0)
  • $timestamp : date and time. You can use the time() function to get the current timestamp.

  • $format : The format of the return value. The default is SUNFUNCS_RET_STRING , returning the string format. If you need to return a timestamp, use SUNFUNCS_RET_TIMESTAMP .

  • $latitude and $longitude : The latitude and longitude of the geographical location. If not provided, the default location of the current device is used by default.

  • $zenith : The zenith corner of the sunset. The default is 90.5 degrees.

  • $gmt_offset : Time zone offset.

2. Get the sunset time of the current date

We can get the sunset time of the current date through the following simple PHP code:

 <?php
// Set the default time zone to Beijing time
date_default_timezone_set('Asia/Shanghai');

// Get the current timestamp
$timestamp = time();

// Set the latitude and longitude of the geographical location(For example,Beijing&#39;s latitude and longitude)
$latitude = 39.9042;  // latitude
$longitude = 116.4074;  // longitude

// Get the sunset time
$sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude);

// Output the sunset time of the current date
echo "Today&#39;s sunset time is: " . $sunset;
?>

In this code, we first get the current timestamp through the time() function, and then set the latitude and longitude of a specific location. Use the date_sunset function to calculate the sunset time of the day of the position and output it.

3. Output format

The return value format of the date_sunset function is determined based on the $format parameter. If we use the default SUNFUNCS_RET_STRING , it returns a string format time. For example:

 Today&#39;s sunset time is: 18:35:21

If you need to return a timestamp, you can set $format to SUNFUNCS_RET_TIMESTAMP as follows:

 $sunset = date_sunset($timestamp, SUNFUNCS_RET_TIMESTAMP, $latitude, $longitude);
echo "Today&#39;s sunset timestamp is: " . $sunset;

The result returned in this way will be a timestamp, which you can format as you want to whatever format you want.

4. Handle time zone issues

The time returned by the date_sunset function is based on UTC time. If you need to output by local time zone, make sure that the correct time zone has been set. The time zone can be set through date_default_timezone_set() , as shown below:

 date_default_timezone_set('Asia/Shanghai');  // Set as Shanghai time

Make sure to set the time zone before calling date_sunset so you can get the exact local time.

5. Complete sample code

 <?php
// Set the default time zone to Beijing time
date_default_timezone_set('Asia/Shanghai');

// Get the current timestamp
$timestamp = time();

// Set the latitude and longitude of the geographical location(For example,Beijing&#39;s latitude and longitude)
$latitude = 39.9042;  // latitude
$longitude = 116.4074;  // longitude

// Get the sunset time
$sunset = date_sunset($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude);

// Output the sunset time of the current date
echo "Today&#39;s sunset time is: " . $sunset;
?>

6. Summary

Through the date_sunset function, PHP developers can easily obtain the sunset time of a specified location and time. It should be noted that you must make sure that the correct time zone and position coordinates are set. In addition, the time returned by this function is based on UTC time, and time zone differences should be taken into account when using it.