Current Location: Home> Latest Articles> Example of how to get the sunset time every day for the entire month

Example of how to get the sunset time every day for the entire month

M66 2025-05-31

In many applications, getting the daily sunset time is very important for some applications involving astronomy, geography, or lifestyle. For example, some apps may schedule tasks based on sunset times or remind users when they start nighttime activities. This article will explain how to use PHP to get the sunset time of the entire month every day, with detailed code examples.

Get the basic principles of sunset time

Obtaining sunset time is usually calculated using astronomical algorithms. A common approach is to use the SunCalc library, a tool for calculating the rise and fall time of a specific location through longitude and latitude. We can get the sunset time of each day by sending a request to the SunCalc API.

In this post, we will get the sunset time of a specific location by calling an API and use PHP code to show how this data is processed.

PHP Code Example

Here is a PHP code example that shows how to get the sunset time of a specified location through the API. We will get the sunset time every day for one month and output it. Here we assume that the latitude and longitude of the target location is known.

 <?php

// Set the latitude and longitude of the target location
$latitude = 40.7128;  // For example:The Latitude of New York
$longitude = -74.0060; // For example:Longitude in New York

// TargetAPI URL,Replace with m66.net domain name
$url = "https://api.m66.net/sunrise-sunset?lat=$latitude&lng=$longitude&date=today&formatted=0";

// Get the sunset time of the current month
function getSunsetTimes($latitude, $longitude) {
    // Current month
    $currentMonth = date('m');

    // Store the sunset time every day
    $sunsetTimes = [];

    // Get the sunset time of every day
    for ($day = 1; $day <= 31; $day++) {
        // Format date
        $date = date('Y-m') . '-' . str_pad($day, 2, '0', STR_PAD_LEFT);

        // Construct requestURL
        $apiUrl = "https://api.m66.net/sunrise-sunset?lat=$latitude&lng=$longitude&date=$date&formatted=0";

        // StartAPIask
        $response = file_get_contents($apiUrl);

        // Check if the response is valid
        if ($response === FALSE) {
            echo "Error fetching data for $date\n";
            continue;
        }

        // AnalysisJSONresponse
        $data = json_decode($response, true);

        // Get the sunset time
        if (isset($data['results']['sunset'])) {
            $sunsetTimes[$date] = $data['results']['sunset'];
        } else {
            echo "No sunset data available for $date\n";
        }
    }

    return $sunsetTimes;
}

// Call the function and output the result
$sunsetTimes = getSunsetTimes($latitude, $longitude);

echo "The sunset time is as follows:\n";
foreach ($sunsetTimes as $date => $sunsetTime) {
    echo "$date: $sunsetTime\n";
}

?>

Code parsing

  1. Set latitude and longitude : In the code, we set the latitude and longitude of New York (40.7128, -74.0060). You can modify the latitude and longitude of other places as needed.

  2. API Request : We get the sunset time for a specific date by sending a request to https://api.m66.net/sunrise-sunset . Note that the response of the API is returned in JSON format, so we use the json_decode function to parse the response data.

  3. Get sunset time : By looping, we get the sunset time of each day of the current month and store it in the $sunsetTimes array, and finally output the result.

  4. Date formatting : We use the date() function to get the current month and format the date in Ymd format.

  5. Output result : Finally, the code outputs the sunset time of each day. If there is no data on a certain day (for example, the result returned by the API is empty), an error message will be output.

Summarize

With this article and code example, you can easily get the sunset times for each day of a designated location throughout the month. Just adjust the latitude and longitude and make sure the URL of the API request is correct and it can be applied to any location and date range. This method is not only suitable for weather-related applications, but also for agriculture, tourism or other scenarios that require sunshine information.

I hope this article will be helpful to you. If you have any questions, please feel free to ask them!