W3docs

date_sun_info()

Learn how PHP's date_sun_info() returns sunrise, sunset, transit, and twilight times for a date and location, with syntax, parameters, and examples.

Introduction

PHP's date_sun_info() function returns the sunrise, sunset, transit, and twilight times for a specific date and geographic location. Instead of pulling sun data from an external service, you pass a timestamp and a pair of coordinates and get back a ready-to-use array of Unix timestamps. This page covers its syntax, parameters, return values, and a complete working example.

Note: date_sun_info() was deprecated in PHP 8.1 and removed in PHP 8.2. This tutorial is provided for legacy code maintenance. For modern PHP applications, consider an external service (such as the Sunrise-Sunset API) or a dedicated library like sunrise-sunset-php.

Syntax

date_sun_info(int $timestamp, float $latitude, float $longitude): array

Parameters

ParameterTypeDescription
$timestampintA Unix timestamp (in UTC) identifying the day to query.
$latitudefloatLatitude of the location, in degrees. Positive = north, negative = south.
$longitudefloatLongitude of the location, in degrees. Positive = east, negative = west.

Return value

The function returns an associative array of Unix timestamps. Each key marks a sun event for that day at the given coordinates:

KeyMeaning
sunriseTime the sun crosses the horizon at dawn.
sunsetTime the sun crosses the horizon at dusk.
transitSolar noon — the sun is at its highest point.
civil_twilight_begin / civil_twilight_endSun 6° below the horizon (outdoor activity still possible).
nautical_twilight_begin / nautical_twilight_endSun 12° below the horizon (horizon visible at sea).
astronomical_twilight_begin / astronomical_twilight_endSun 18° below the horizon (sky fully dark).

If the sun never rises or never sets on that day (for example, polar regions in summer or winter), the relevant value is true (sun is up all day) or false (sun never rises) instead of a timestamp.

Using the date_sun_info() Function

Because the first argument is a Unix timestamp, the simplest approach is to build a DateTime object for the day you care about and pass its timestamp via getTimestamp(). The flow looks like this:

graph TD;
    A[Create DateTime object] --> B[Get timestamp for the date];
    B --> C[Call date_sun_info with timestamp + coordinates];
    C --> D[Read sunrise / sunset / twilight from the result];

Here's a complete example for London, UK:

php— editable, runs on the server

In this example, we create a DateTime object that represents a specific date, and we specify the latitude and longitude of London, UK. We then call the date_sun_info() function and pass in the timestamp of the DateTime object, along with the latitude and longitude.

The date_sun_info() function returns an array that contains information about the sunrise, sunset, and other related times for the specified date and location. Here's an example of the output:

Array
(
    [sunrise] => 1646242025
    [sunset] => 1646282555
    [transit] => 1646262290
    [civil_twilight_begin] => 1646239733
    [civil_twilight_end] => 1646284847
    [nautical_twilight_begin] => 1646235838
    [nautical_twilight_end] => 1646288742
    [astronomical_twilight_begin] => 1646231998
    [astronomical_twilight_end] => 1646292582
)

The values are raw Unix timestamps, so to display them you format each one with date() or date_format():

<?php
$sun_info = date_sun_info(strtotime('2023-03-03'), 51.5074, -0.1278);
echo 'Sunrise: ' . date('H:i:s', $sun_info['sunrise']) . "\n";
echo 'Sunset:  ' . date('H:i:s', $sun_info['sunset']) . "\n";
?>

Note on time zones: The function expects a Unix timestamp (UTC). The returned times are also UTC timestamps, calculated from the supplied coordinates regardless of your server's local time zone. When you format them with date(), the output uses the time zone set by date_default_timezone_set(), so set it explicitly if you need local clock times.

Conclusion

date_sun_info() returns sunrise, sunset, transit, and the three twilight phases for a given date and location as an array of UTC Unix timestamps. Pass a timestamp plus latitude and longitude, then format the returned values with date() to display them. It is handy for weather, scheduling, and location-aware features — but remember it was removed in PHP 8.2, so reach for an external API or library in modern code.

Practice

Practice
What does the 'date_sun_info' function do in PHP?
What does the 'date_sun_info' function do in PHP?
Was this page helpful?