JoeR JoeR - 4 months ago 30x
PHP Question

PHP date_sun_info wrong times

I'm trying to use PHP's

function to get information about the time of certain positions of the sun throughout the day:

At the moment I'm using code similar to that in the documentation.

$sun_info = date_sun_info(strtotime('today'), 40.42, 74.0);
foreach ($sun_info as $key => $val) {
echo "$key: " . date("H:i:s", $val) . "<br>";

Output is:

sunrise: 20:50:20
sunset: 07:45:03
transit: 02:17:41
civil_twilight_begin: 20:22:45
civil_twilight_end: 08:12:38
nautical_twilight_begin: 19:51:01
nautical_twilight_end: 08:44:22
astronomical_twilight_begin: 19:19:28
astronomical_twilight_end: 09:15:55

Which is obviously wrong.

I'm unsure why this happens. Any help would be much appreciated.

I had thought that it is something to do with timezones? If so how should I correct for this? The timezone for the server is set to America/New_York which would be 5 hours behind GMT but even taking that into account the times cannot be right, unless I'm calculating this wrong.


I think you mixed up the numbers. 40.42, 74.0 translates to a point in the mountains of Kyrgysztan. You probably meant 40.42, -74 for (approximately) New York?

As to why this happens, it seems like the function is indeed time zone sensitive, something that is not properly documented:

  • Kyrgyzstan is GMT+6
  • New York is GMT-5

that amounts to a difference of 11 hours.

Sunrise time at that point is 7:50 local time, 20:50 New York time.