When working with PHP, you may need to calculate approximate sunrise and sunset timestamps. This guide explains the operation, shows what its arguments mean, and uses three separate examples to make the result easier to understand.
date_sun_info: The array includes sunrise, sunset and twilight timestamps for a geographic location.
What Is date_sun_info in PHP?
Calculate approximate sunrise and sunset timestamps. The array includes sunrise, sunset and twilight timestamps for a geographic location.
Syntax:
date_sun_info(int $timestamp, float $latitude, float $longitude): array
Parameters and return value: The signature above identifies the arguments and return type where the API has a single call. For topic guides, follow the sequence of calls in the examples. Always distinguish a returned value from an object or variable modified by reference.
Important: At high latitudes some solar events do not occur on a given day. Check for false values.
Example 1: Basic Usage
This example introduces the operation with a short and predictable input.
<?php
$result = date_sun_info(strtotime("2026-06-01 UTC"), 51.5074, -0.1278);
var_dump($result["sunrise"]);
Output:
int(1780285722)
The code focuses on the main operation. The array includes sunrise, sunset and twilight timestamps for a geographic location.
Example 2: Another Case
Now compare a different input, locale, date, or option to see another part of the behavior.
<?php
$result = date_sun_info(strtotime("2026-12-01 UTC"), 51.5074, -0.1278);
var_dump($result["sunset"]);
Output:
int(1796140514)
What changes? At high latitudes some solar events do not occur on a given day. Check for false values.
Practical Example: Applying It in an Application
Use the following separate example as a starting point for a small application task. Check any extension or environment requirements below before running it.
<?php
$location = ["latitude" => 40.7128, "longitude" => -74.0060];
$sun = date_sun_info(strtotime("2026-07-01 UTC"), $location["latitude"], $location["longitude"]);
foreach (["sunrise", "sunset"] as $event) {
$timestamp = $sun[$event];
echo $event . ": " . (is_int($timestamp) ? gmdate("H:i", $timestamp) . " UTC" : "unavailable") . PHP_EOL;
}
Output:
sunrise: 09:28 UTC
sunset: 00:31 UTC
The practical example demonstrates how to apply this operation to multiple values, a reusable task, or a validated input. The array includes sunrise, sunset and twilight timestamps for a geographic location.
Important Behavior and Edge Cases
- Core behavior: The array includes sunrise, sunset and twilight timestamps for a geographic location.
- Inputs and results: At high latitudes some solar events do not occur on a given day. Check for false values.
- Alternatives: date_sunrise() and date_sunset() are narrower alternatives.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
date_sun_info | Calculate approximate sunrise and sunset timestamps. |
| Related PHP approach | date_sunrise() and date_sunset() are narrower alternatives. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using an incorrect input or option | At high latitudes some solar events do not occur on a given day. Check for false values. |
| Confusing this API with a related operation | date_sunrise() and date_sunset() are narrower alternatives. |
| Assuming output is identical across environments | Check return values and required extensions or versions. Use explicit locale, timezone or encoding settings when they affect the result. |
Key Takeaways
- date_sun_info helps you calculate approximate sunrise and sunset timestamps.
- The array includes sunrise, sunset and twilight timestamps for a geographic location.
- At high latitudes some solar events do not occur on a given day. Check for false values.
Use date_sun_info when you need to calculate approximate sunrise and sunset timestamps. Start with the first example, compare the second, and adapt the practical example to your inputs. Confirm the return value and environment requirements before adding it to production code.