PHP date_sunrise() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, date_sunrise() returns the time of sunrise for a given day and location, but it is deprecated as of PHP 8.1.

You can ask for the result as a string, a Unix timestamp, or a number of hours. New code should use date_sun_info(), which returns sunrise, sunset, and twilight times in one array.

Syntax

Call date_sunrise() with the values you need. Start with this line.

PHP
$time = date_sunrise(1718971200, SUNFUNCS_RET_STRING, 51.5074, -0.1278, 90.833333, 0);
CallWhat it does
date_sunrise($ts, SUNFUNCS_RET_STRING, 51.5074, -0.1278, 90.833333, 0)Returns a time string like 03:43.
date_sunrise($ts, SUNFUNCS_RET_TIMESTAMP, 51.5074, -0.1278, 90.833333, 0)Returns the sunrise as a Unix timestamp (an int).
date_sunrise($ts, SUNFUNCS_RET_DOUBLE, 51.5074, -0.1278, 90.833333, 0)Returns the sunrise as hours after midnight (a float).

DEPRECATED as of PHP 8.1: calling date_sunrise() triggers a deprecation message, and it may be removed in a future version. Use date_sun_info() instead. The examples below hide or capture that message so the output stays clean. The time shown is approximate.

Library declarations
PHP
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

Parameters

ParameterDescription
timestampRequired. A Unix timestamp that selects the day.
returnFormatOptional. SUNFUNCS_RET_STRING (default, text like 03:43), SUNFUNCS_RET_TIMESTAMP (int), or SUNFUNCS_RET_DOUBLE (float hours).
latitudeOptional. Degrees north. Defaults to the ini setting date.default_latitude.
longitudeOptional. Degrees east. Defaults to the ini setting date.default_longitude.
zenithOptional. The sun angle that counts as sunrise. Use about 90.833333 for the standard definition.
utcOffsetOptional. The timezone offset in hours (for example 0 for UTC or 5.5 for India).

Return Value

Return ValueDescription
string|int|floatThe sunrise time in the requested format.
falseReturned when there is no sunrise on that day, such as in the Arctic summer.

PHP Examples

Example 1 — Sunrise as a String (with Deprecation Notice)

On PHP 8.1 and later the function shows a deprecation message. This example catches it with an error handler so you can see it.

PHP
<?php
date_default_timezone_set("UTC");

set_error_handler(function ($severity, $message) {
    echo "Deprecated: ", $message, PHP_EOL;
    return true;
}, E_DEPRECATED);

$timestamp = 1718971200; // 2024-06-21 12:00:00 UTC
$sunrise = date_sunrise($timestamp, SUNFUNCS_RET_STRING, 51.5074, -0.1278, 90.833333, 0);
echo "London sunrise (UTC): ", $sunrise, PHP_EOL;

restore_error_handler();

How It Works

  1. set_error_handler() catches deprecation messages so we can print them instead of letting PHP show them.
  2. date_sunrise() is called with the London coordinates, a standard zenith, and a UTC offset of 0.
  3. On PHP 8. 1 and later the handler prints the deprecation notice. On older versions that first line does not appear.
  4. The string result 03:43 is the approximate sunrise time in UTC.

Example 2 — Return Formats and No Sunrise

Compare the three return formats. In the Arctic summer there is no sunrise, so the result is false.

PHP
<?php
error_reporting(E_ALL & ~E_DEPRECATED); // hide the 8.1+ deprecation message
date_default_timezone_set("UTC");

$timestamp = 1718971200;
$asInt = date_sunrise($timestamp, SUNFUNCS_RET_TIMESTAMP, 51.5074, -0.1278, 90.833333, 0);
$asFloat = date_sunrise($timestamp, SUNFUNCS_RET_DOUBLE, 51.5074, -0.1278, 90.833333, 0);
$polar = date_sunrise($timestamp, SUNFUNCS_RET_STRING, 69.6492, 18.9553, 90.833333, 0);

echo gettype($asInt), PHP_EOL;
echo gettype($asFloat), " ", round($asFloat, 1), PHP_EOL;
echo "Polar: ", var_export($polar, true), PHP_EOL;

How It Works

  1. error_reporting() hides the deprecation message so only our output is shown.
  2. SUNFUNCS_RET_TIMESTAMP returns an integer timestamp.
  3. SUNFUNCS_RET_DOUBLE returns hours after midnight, about 3. 7 for 03:43.
  4. In Tromso on June 21 the sun never sets, so there is no sunrise and PHP returns false.

Example 3 — The Modern Way: date_sun_info()

Replace date_sunrise() with date_sun_info(). It returns a timestamp that you format with date().

PHP
<?php
date_default_timezone_set("UTC");

$timestamp = 1718971200;
$info = date_sun_info($timestamp, 51.5074, -0.1278);

echo "Sunrise: ", date("H:i", $info["sunrise"]), PHP_EOL;
echo "Sunset: ", date("H:i", $info["sunset"]), PHP_EOL;

How It Works

  1. date_sun_info() takes the timestamp, latitude, and longitude. There is no zenith or offset to remember.
  2. It returns an array, so one call covers both sunrise and sunset.
  3. date("H:i") formats each timestamp using the default timezone.
  4. The sunrise matches what date_sunrise() gave in the first example, without the deprecation message.

Frequently Asked Questions

Yes. It is deprecated as of PHP 8.1 and shows a deprecation message when called. Use date_sun_info() in new code.
Use date_sun_info(). It returns the sunrise, sunset, solar noon, and twilight times in one array, and the sunrise value is a timestamp you can format with date().
SUNFUNCS_RET_STRING returns text like 03:43, SUNFUNCS_RET_TIMESTAMP returns a Unix timestamp, and SUNFUNCS_RET_DOUBLE returns hours as a float, such as 3.72.
It returns false when the sun does not rise on that day at that location, such as during polar summer or polar night.
Use about 90.833333, which accounts for the size of the sun and the bending of light by the atmosphere. Other values give civil or nautical twilight instead.

Next: date_sunset()

date_sunset() returns the time of sunset for a given day and location, but it is deprecate.

date_sunset() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful