W3docs

date_sunrise()

PHP is a widely used programming language that is commonly used to create dynamic web applications. The func_date_sunrise() function is one of the many built-in

The Importance of PHP date_sunrise() Function

Note: The date_sunrise() function was deprecated in PHP 5.2.0 and removed in PHP 8.0.0. The content below is provided for legacy PHP versions only.

PHP is a widely used programming language that is commonly used to create dynamic web applications. The date_sunrise() function is one of the many built-in functions that are available in PHP. This function is used to calculate the time of sunrise for a given date and location.

The date_sunrise() function is an essential tool for developers who are building applications that require sunrise times. This function can be used to calculate the sunrise time for any location on Earth, based on its latitude and longitude.

Syntax

date_sunrise(
    int $timestamp,
    int $returnFormat = SUNFUNCS_RET_STRING,
    ?float $latitude = null,
    ?float $longitude = null,
    ?float $zenith = null,
    ?float $utcOffset = null
): string|int|float|false

How to Use the PHP date_sunrise() Function

The date_sunrise() function takes one required parameter and up to five optional ones.

ParameterDescription
timestampThe Unix timestamp of the day for which you want the sunrise time.
returnFormatOne of SUNFUNCS_RET_STRING (a hh:mm string), SUNFUNCS_RET_DOUBLE (hours as a float), or SUNFUNCS_RET_TIMESTAMP (a Unix timestamp).
latitudeLatitude in degrees. Defaults to the date.default_latitude INI setting.
longitudeLongitude in degrees. Defaults to the date.default_longitude INI setting.
zenithThe angle between the center of the sun and the horizon. Defaults to date.sunrise_zenith (90°50′).
utcOffsetOffset from UTC in hours. Only used with SUNFUNCS_RET_STRING and SUNFUNCS_RET_DOUBLE.

Return value: the sunrise time in the chosen returnFormat, or false on failure (for example, in polar regions where the sun does not rise on the given day).

To use the function, first obtain the latitude and longitude coordinates for the location you are interested in — online tools such as Google Maps or GeoNames make this easy. Then call date_sunrise() with those coordinates, and it returns the time of sunrise for the specified location and date.

The companion function date_sunset() calculates sunset times with an identical signature.

Examples of Using the PHP date_sunrise() Function

Here are some examples of using the date_sunrise() function:

Example 1: Calculating the sunrise time for today at a specific location

php— editable, runs on the server

Example 2: Calculating the sunrise time for a specific date at a specific location. Here strtotime() converts a human-readable date into the Unix timestamp that date_sunrise() expects.

<?php
// Note: date_sunrise() was removed in PHP 8.0.0. This code is for legacy PHP versions only.
$date = "2023-03-03";
$timestamp = strtotime($date);
$latitude = 40.7128; // New York City latitude
$longitude = -74.0060; // New York City longitude
$zenith = ini_get("date.sunrise_zenith");
$sunrise_time = date_sunrise($timestamp, SUNFUNCS_RET_STRING, $latitude, $longitude, $zenith);

if ($sunrise_time !== false) {
    echo "The sunrise time for New York City on $date is $sunrise_time.";
} else {
    echo "Failed to calculate sunrise time.";
}
?>

Modern Alternatives for PHP 8+

Since PHP 8.0 removed built-in astronomical calculations, modern applications typically rely on dedicated libraries or external APIs. Common approaches include:

  • Composer packages like geo-sun or sunrise-sunset for local, dependency-free calculations.
  • External APIs such as Sunrise-Sunset.org or Open-Meteo for reliable, up-to-date data without maintaining local algorithms.

Mermaid Diagram: Sunrise Calculation Process

graph LR;
    A[Enter latitude and longitude coordinates] --> B{Call date_sunrise() function};
    B -- Returns the sunrise time --> C[Display sunrise time];

Conclusion

In conclusion, the date_sunrise() function calculates the sunrise time for a given location and date on legacy PHP versions. Because it was removed in PHP 8.0, modern projects should reach for a Composer package or an external API instead. For related date and time handling, see date(), mktime(), and date_default_timezone_set().

Practice

Practice
What does the date_sunrise() function in PHP do?
What does the date_sunrise() function in PHP do?
Was this page helpful?