Skip to content

Since PHP 8, the date_sun_info() function returns inaccurate sunrise and sunset times, but other calculated times are correct #18076

Closed
@JiriJozif

Description

@JiriJozif

Description

The following code:

<?php
date_default_timezone_set("UTC");
$sun_info = date_sun_info(strtotime("2025-03-21"), 51.48, 0.0);
echo date("H:i:s\n", $sun_info['sunrise']);
echo date("H:i:s\n", $sun_info['sunset']);

Resulted in this output PHP 7.4 (compare https://aa.usno.navy.mil/calculated/rstt/oneday?date=2025-03-21&lat=51.48&lon=0.0000&label=&tz=0.00&tz_sign=1&tz_label=false&dst=false&submit=Get+Data):

05:59:21
18:14:48

Resulted in this output PHP 8.3.17:

05:57:45
18:16:25

I think the problem is in the call function timelib_astro_rise_set_altitude() in /ext/date/php_date.c
Back in PHP 7.4.33 it was called like this
timelib_astro_rise_set_altitude(t, longitude, latitude, -35.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
https://github.com/php/php-src/blob/PHP-7.4.33/ext/date/php_date.c#L5146
The fourth parameter, -35.0/60, is the refraction value in degrees.
In the timelib_astro_rise_set_altitude() function then subtracts the calculated apparent radius of the Sun (which is approximately 15.0/60)
https://github.com/php/php-src/blob/PHP-7.4.33/ext/date/lib/astro.c#L260 (the variable upper_limb is always 1)
The result is the correct topocentric altitude (-50/60 degrees) needed to calculate the sunrise and sunset times.

However, since PHP 8, it is called timelib_astro_rise_set_altitude(t, longitude, latitude, -50.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
https://github.com/php/php-src/blob/PHP-8.0/ext/date/php_date.c#L4663
The fourth parameter, -50.0/60, is refraction and apparent radius together. But in the function itself, the subtraction of the calculated apparent radius of the Sun is still done.
The resulting topocentric altitude (-65/60 degrees) is wrong and consequently so are the calculated sunrise and sunset times. The other calculated times (transit and *twilight*) are correct.

PHP Version

PHP 8.3.17

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions