Why isn't date_sun_info() working correctly?


this code

$date = '2010-03-27';

$ts = strtotime($date);

$date_sun_info = date_sun_info ( $ts, 51.5, 0 );

echo '<p>', $date, '</p>';
echo '<table>';
foreach( $date_sun_info as $k => $v )
	echo '<tr><td>', $k, '</td><td>', date('H:i', $v), '</td></tr>';
echo '</table>';



sunrise				02:35
sunset				14:42
transit				08:39
civil_twilight_begin		02:15
civil_twilight_end		15:03
nautical_twilight_begin		01:51
nautical_twilight_end		15:27
astronomical_twilight_begin	01:27
astronomical_twilight_end	15:51

51.5, 0 (latitude, longitude) is london. the sun did not rise at 2:35am in london today. more like 6:40am give or take a few minutes. why is it outputing the wrong info?


What timezone do you have PHP set to use?

i’m not sure. it’s a shared hosting server. some possibly relevant bits from phpinfo()'s output in the ‘date’ section:

date/time support: enabled
Timezone Database Version: 0.system
Timezone Database: internal
Default timezone: System/Localtime
date.timezone: no value

ah, just found out about echo date(“T”). that outputs ‘GMT’. so that would seem correct.

$date_sun_info = date_sun_info ( time(), 51.5, 0 );
Does that work? or what about
$date = ‘2010-27-03’;

no, same result (although now, seeing as we’ve just gone into european summer time all the values have got an hour added on, so the sunrise is 03:35 for example, but it’s still wrong in the same way).

i didn’t try $date = ‘2010-27-03’ because i know strtotime is recognising ‘2010-03-27’ fine (i used the resulting timestamp in a date function and it was fine) but i did try time() instead of the time stamp from strtotime() – no difference

$today = date("D M j G:i:s T Y");  
echo $today;

What time is echoed by that (current UK time is 03:02)?

It’s still up to you to determine which timezone that lat/long belongs to, and then do the appropriate conversion to the current local time of that location. The function just gives you the GMT time.

I don’t know of an easy, good way to do that. I suspect you’ll need a database that you keep updated.

it just output Sun Mar 28 10:44:27 BST 2010 which is correct now in the uk.

right (so what you’re saying is it’ll give the right times but local to where the server is, not local to where the lat/long is) but i’m in the uk and i’m using a lat/long in the uk and the time zone of the server seems to be set to GMT (edit: correction, BST now - british summer time) which is correct for the uk.

maybe this is it. in phpinfo()'s output (i should have paid more attention to it last night) there’s this:

date.default_latitude: 31.7667
date.default_longitude: 35.2333

that’s jerusalem :confused: i wonder if jarusalem is 4 hours ahead of the uk, i bet it is. why on earth would the server be set to that? doesn’t matter, but how do i set it to a uk lat/long? is it something i can set in .htaccess, or maybe i should ask the hosting company to correct it? maybe the server really is in jerusalem. don’t think so, they’re called ukservers, but who knows.

also beneath that lat/long bit in phpinfo is:
date.sunrise_zenith: 90.583333
date.sunset_zenith: 90.583333

don’t know what that is but maybe it could also set incorrectly? clearly it’s related to the kind of info date_sun_info() provides.

I think I’ve found it, a google search turned up a bug with the function where the longitude and latitude were reversed. Try reversing the longitude and latitude, if that works swap over the variables that you use to pass the longitude and latitude to the function. Don’t forget to add a comment to remind yourself why so that you know what to change back for whenever you move to a newer version of php.

yes, that’s it, thanks SpacePhoenix. it outputs this now:

sunrise 06:43
sunset 19:26
transit 13:04
civil_twilight_begin 06:09
civil_twilight_end 20:00
nautical_twilight_begin 05:29
nautical_twilight_end 20:40
astronomical_twilight_begin 04:46
astronomical_twilight_end 21:23

haven’t checked it thoroughly but the sunrise and sunset looks right. so it wasn’t the fact my server seems to be saying it’s in jerusalem (quite funny that) after all. pretty crap bug. anyway, all’s well now, excellent, thanks.