Current Location: Home> Latest Articles> How to Calculate Civil, Nautical, and Astronomical Sunset Times Using Custom Zenith Values

How to Calculate Civil, Nautical, and Astronomical Sunset Times Using Custom Zenith Values

M66 2025-07-07

When calculating sunset times, a specific angle known as the Zenith value is typically used to describe the relative position of the sun. The Zenith value is an important parameter in the calculation of sunset, sunrise, or other astronomical events. Different Zenith values represent different astronomical events such as civil sunset, nautical sunset, and astronomical sunset, each corresponding to different standards of visibility and darkness. This article will explain how to use PHP programming to calculate civil, nautical, and astronomical sunset times based on different Zenith values.

1. What is the Zenith Value?

The Zenith value, or "astronomical angle," refers to the angle of the sun relative to the horizon. Specifically, the Zenith value represents the angle of the center of the sun from the horizon. Different Zenith values are used to represent different astronomical events:

  • Civil Sunset: The Zenith value is usually 96°, representing the moment when the sun drops below the horizon by 96 degrees. This typically marks the time when the sky begins to darken, usually from half an hour to an hour after sunset.

  • Nautical Sunset: The Zenith value is 102°, representing the moment when the sun drops below the horizon by 102 degrees. This is typically used in navigation to define "visibility after sunset."

  • Astronomical Sunset: The Zenith value is 108°, indicating that the sun is completely below the horizon, and the sky becomes entirely dark, suitable for astronomical observation.

2. How to Calculate Sunset Time?

Calculating sunset times typically involves a series of complex astronomical formulas, but we can simplify the process by using existing open-source PHP libraries. We will use a simple calculation method based on latitude, longitude, and Zenith angle.

2.1 Writing a PHP Program to Calculate Sunset Time

We will write a PHP program that inputs a date and location (latitude and longitude), as well as a Zenith value, to calculate different types of sunset times. We assume that we have a PHP library to calculate sunset times. Below are the steps of the program:

<?php
// Include the sunset time calculation library
include('sunset_calculator.php'); 
<p>// Set latitude and longitude (e.g., Beijing's coordinates)<br>
$latitude = 39.9042;<br>
$longitude = 116.4074;<br>
$date = '2025-04-26';  // Set the date</p>
<p>// Define different Zenith values<br>
$zenith_values = [<br>
'civil' => 96,    // Civil sunset<br>
'nautical' => 102, // Nautical sunset<br>
'astronomical' => 108, // Astronomical sunset<br>
];</p>
<p>// Calculate sunset times based on different Zenith values<br>
foreach ($zenith_values as $type => $zenith) {<br>
$sunset_time = getSunsetTime($latitude, $longitude, $date, $zenith); // Calculate the sunset time<br>
echo "On {$date}, the sunset time using {$type} (Zenith={$zenith}°) is: {$sunset_time}\n";<br>
}</p>
<p>// Function to calculate sunset time<br>
function getSunsetTime($lat, $lon, $date, $zenith) {<br>
// Call an API to get the sunset time, such as m66.net's API<br>
$url = "<a rel="noopener" target="_new" class="cursor-pointer">https://api.m66.net/sunset?latitude={$lat}&longitude={$lon}&date={$date}&zenith={$zenith}</a>";</p>
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

// Parse the returned data and return the sunset time
$data = json_decode($response, true);
return $data['sunset_time'];

}
?>

2.2 Program Explanation

  1. Setting Latitude and Longitude: We input the latitude and longitude of a specific location (e.g., Beijing) and set the date.

  2. Different Zenith Values: We define the different types of Zenith values, which are 96°, 102°, and 108°.

  3. Calculating Sunset Time: The program uses the getSunsetTime function, passing in the latitude, longitude, date, and Zenith value to calculate the sunset time.

  4. API Request: We make an API call (assuming there is an m66.net API) to calculate and return the sunset time.

  5. Output Result: The program outputs the civil, nautical, and astronomical sunset times based on the different Zenith values.

3. API Calls for Sunset Time

In the actual program, we call an external API to get the accurate sunset time. For example, in the getSunsetTime function, we use a hypothetical API https://api.m66.net/sunset to calculate the sunset time.

The API request passes the latitude, longitude, date, and Zenith angle. The API then returns the calculated sunset time. This API can dynamically calculate based on the user-provided data and return the result to the program.