There are two thing开发者_如何学JAVAs I do not understand about this function:
1.Offset not being taken into account when using in the format SUNFUNCS_RET_TIMESTAMP. For example if I use for the format SUNFUNCS_RET_STRING and use this code:
$lat = 46.055556; // Latitude (Ljubljana).
$long = 14.508333; // Longitude (Ljubljana).
$offset = 2; // Difference between GMT and local time in hours.
$zenith = 90 + 50 / 60;
echo "<br><p>Sunrise: " . date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
echo "<br>Sunset: " . date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
Then it will display somehow correct answear (by what I mean somehow will explain in second point):
Sunrise: 05:15 Sunset: 20:42
Now if I use SUNFUNCS_RET_TIMESTAMP and convert it to string representation of date with function date():
$lat = 46.055556; // Latitude (Ljubljana).
$long = 14.508333; // Longitude (Ljubljana).
$offset = 2; // Difference between GMT and local time in hours.
$zenith = 90 + 50 / 60;
$dateSunRise = date_sunrise(time(), SUNFUNCS_RET_TIMESTAMP, $lat, $long, $zenith, $offset);
$dateSunSet = date_sunset(time(), SUNFUNCS_RET_TIMESTAMP, $lat, $long, $zenith, $offset);
echo "<br><p>Sunrise: " . date("H:i", $dateSunRise);
echo "<br><p>Sunrise: " . date("H:i", $dateSunSet);
I get this result:
Sunrise: 03:15 Sunrise: 18:42
I checked the timestamp and it sure returns wrong, which is why I do not understand, because I have the same offset defined as in previous example where I used SUNFUNCS_RET_STRING.
2.In first point I have written, that it displays somehow correct answear, here is an explaination by what I mean by that. Our official meteorological agency ARSO updates daily on their pages the information about sunrise and sunset. The page can be found on this link (sorry, because it is not in english). Bottom line is, that for our capital city (Ljubljana) it has this information:
Sunrise: 5:18 Sunrise: 20:41
Where I get with date_sunrise and date_sunset function this:
Sunrise: 05:15 Sunset: 20:42
The parameters are the same as in first point (latitude, longitude, offset, zenith). Now the latitude and longitude are certainly correct (checked with GeoHack for Ljubljana). I am starting to think that this diffrence comes from zenith parameter. But I have searched on the internet and it says this:
The best Overall figure for zenith is 90+(50/60) degrees for true sunrise/sunset
I am clueless why it comes to this diffrence, is it possible that this two functions return wrong result?
When you use timestamps, they're almost always UNIX timestamps, which means they're given in UTC. When working with them, you'll have to convert them to your own timezone at the end of the calculation. For your purposes, you probably just want the SUNFUNCS_RET_STRING.
For the second point, zenith plays a large part in calculating the exact minute of sunrise and sunset. The 90+(50/60)
is just a ballpark estimate. There are calculators online which can give you better numbers to use for your coordinates, and they will be different for both sunrise and sunset.
The following code will give you the numbers you're looking for in Ljubljana:
$lat = 46.055556; // Latitude (Ljubljana).
$long = 14.508333; // Longitude (Ljubljana).
$offset = 2; // Difference between GMT and local time in hours.
$rise_zenith = 90+(25/60);
$set_zenith = 90+(40/60);
echo "<br>Sunrise: " . date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $rise_zenith, $offset);
echo "<br>Sunset: " . date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $set_zenith, $offset);
精彩评论