PHP date_sun_info() Function

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

Definition

In PHP, date_sun_info() returns an array with the sunrise, sunset, solar noon, and twilight times for a given day and place.

You give it a timestamp and the latitude and longitude of a location. It is the modern replacement for the deprecated date_sunrise() and date_sunset() functions.

Syntax

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

PHP
$info = date_sun_info(1718971200, 51.5074, -0.1278);
CallWhat it does
date_sun_info($timestamp, 51.5074, -0.1278)Returns sun times for London on the day of the timestamp.
$info["sunrise"]A timestamp for sunrise, or true or false when the sun never sets or never rises.
$info["transit"]A timestamp for solar noon, when the sun is highest.

Most values are Unix timestamps, so format them with date() after setting your timezone. The times can differ from other sun calculators by about a minute. In polar regions, the values can be true (the sun stays up) or false (the sun stays down) instead of a timestamp.

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

Parameters

ParameterDescription
timestampRequired. A Unix timestamp. PHP uses the day this timestamp falls on.
latitudeRequired. Degrees north of the equator. Use a negative number for the south.
longitudeRequired. Degrees east of Greenwich. Use a negative number for the west.

Return Value

Return ValueDescription
arrayAn array with the keys sunrise, sunset, transit, civil_twilight_begin, civil_twilight_end, nautical_twilight_begin, nautical_twilight_end, astronomical_twilight_begin, and astronomical_twilight_end.

PHP Examples

Example 1 — See the Array Keys

Every call returns the same nine keys for any location. Print them to see what you can read.

PHP
<?php
date_default_timezone_set("UTC");

$timestamp = 1718971200; // 2024-06-21 12:00:00 UTC
$info = date_sun_info($timestamp, 51.5074, -0.1278);

foreach (array_keys($info) as $key) {
    echo $key, PHP_EOL;
}

How It Works

  1. Set the timezone to UTC so the result does not depend on the server.
  2. The timestamp 1718971200 is noon on June 21, 2024.
  3. date_sun_info() uses the day of the timestamp, plus the latitude and longitude of London.
  4. array_keys() lists the nine entries that you can read from the result.

Example 2 — Midnight Sun and Polar Night

Near the poles the sun can stay up or down all day, so PHP returns true or false instead of a time.

PHP
<?php
date_default_timezone_set("UTC");

$summer = date_sun_info(1718971200, 69.6492, 18.9553);
echo "Summer sunrise: ", var_export($summer["sunrise"], true), PHP_EOL;

$winter = date_sun_info(1734782400, 69.6492, 18.9553);
echo "Winter sunrise: ", var_export($winter["sunrise"], true), PHP_EOL;

How It Works

  1. The coordinates 69. 6492, 18.9553 are in Tromso, Norway, north of the Arctic Circle.
  2. On June 21 the sun never sets, so sunrise is true (the sun is always up).
  3. On December 21 (timestamp 1734782400) the sun never rises.
  4. The sunrise value is false, so you must check the type before calling date().

Example 3 — Print Readable Sun Times

Format the sunrise, solar noon, and sunset timestamps with date(). The times are approximate.

PHP
<?php
date_default_timezone_set("UTC");

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

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

How It Works

  1. Call date_sun_info() for London on June 21, 2024.
  2. The sunrise value is a timestamp, so date("H:i") turns it into hours and minutes.
  3. Solar noon is a little after 12:00 UTC because London is slightly west of Greenwich.
  4. All times are in UTC. In summer London clocks are one hour ahead, so the local sunrise is about 04:43.

Frequently Asked Questions

It returns an array with timestamps for sunrise, sunset, solar noon (transit), and the start and end of civil, nautical, and astronomical twilight for a place and day.
Use date_sun_info(). The functions date_sunrise() and date_sunset() are deprecated as of PHP 8.1, and date_sun_info() gives you all the sun times in one call.
In polar regions the sun may stay above the horizon all day (true) or below it all day (false). Twilight values can also be false in places where it never gets that dark.
Set the timezone with date_default_timezone_set() and then use date("H:i", $info["sunrise"]). The timestamp is an exact moment, so date() converts it for you.
No. They are calculated, so they can be off by a minute or so compared with other sun calculators. They are good enough for most websites and apps.

Next: date_sunrise()

date_sunrise() returns the time of sunrise for a given day and location, but it is depreca.

date_sunrise() 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