I’m looking for a stack that automatically provides daily sunrise and sunset times for a given location, as set by longitude and latitude. Ideally, the total amount of daylight in hours and minutes for that location and date would also be provided. The icing on the cake would also include civil twilight times for that location and date. Conceptually, presentation of the stack’s information might look like this:
Sunrise Today: 7:28 am Sunset Today: 4:23 pm
Daylight
7:28 am – 4:23 pm
8 hours, 55 minutes
Civil Twilight
6:53 am - 7:28 am
4:23 pm - 4:58 pm
All this is for a weather page for an airport’s website; this information is important for pilots as a part of their flight planning, especially in winter months when daylight hours are limited. The source I used for the above sample times is: Sunrise and sunset times in Seattle
Not sure of a stack per se, but what about pulling a NOAA RSS feed that publishes this info?
I found this NOAA site for a town in TN that has the sunrise and sunset data in RSS format. If this works, then there are several stacks that can help.
After a few minutes on the NOAA site, it seems that the National Weather Service provides weather forecast data relevant to towns and cities. But airports like the one I’m working with are nowhere near a town. (I was surprised to learn that sunrise-sunset times change even over relatively short distances.) On the NOAA site, I found no means to narrow the geographic area of a forecast further than a town.
The most accurate way to provide specific location information is by latitude and longitude, though for locating an airport, an ICAO airport code, ie., KSEA (KSEA = Seattle-Tacoma airport), would also meet that need.
But I’ll take anything I can get, so yes, please tell me about stacks with RSS function that might work.
I dabble in Amateur Radio and sunrise and sunset times are important as it’s the best times for pushing a radio signal to extreme distances. (Frequency dependant).
I always like GEOChron clocks and you may find one on the internet to fit your needs. Take a look at this one for example: http://www.fourmilab.ch/cgi-bin/Earth
Here is one for use on a Raspberry PI. It’s free and makes a great project to hangs a large screen on the wall with just this app running. http://kd2iff.com/node/20. Another reasons for pointing you at this page is the small side bar plugin that many Amateur Radio enthusiasts use on their web pages called “Solar Terrestrial Data”. I’m not sure it gives sunrise or sunset times at first look but if you go to the owner website here http://www.hamqsl.com/solar.html, then one of the options is to give it a GeoChron style of view. Again it’s free to use with just a tiny bit of html code.
There are others about that may give you the ability to zoom in to your location and give a more accurate picture.
If’s a very simple PHP Script using the data from the Sunset & Sunrise API, however, calculating the times is pure Math, so any Javascript Library would to the job.
Heiko, what you’ve provided is exactly what I was looking for. Changing $lat and $lng and trimming a few unneeded lines from the php produced the desired result, albeit in UTC+1. As a novice I took a look at tutorials at w3schools for info about creating a simple JS script for the time conversion, but it was over my head. I have no idea how incorporate JS into PHP–or even if that’s how it would be done.
I’m in UTC-8. Could you point me to a way to add a calculation to my timezone?
My bad, the code was using the time zone of the api server.
I have attached the project file with and updated code snipped that uses the time zone of the server who hosts the files, hope this helps.
(If not, we have to convert the time zone)
After applying your updated code with my lat/long, I find that time and date results remain in UTC1 (not UTC-8/Pacific Standard Time where I’m located), so it appears that converting the time zone may be the best solution.
(The formatting of your initial demo PHP file is exactly what I’ve been looking for.)
Sorry to bother you with this; thank you for your assistance!
Verified correct sunrise and sunset times, and hours of daylight for today’s date (11/28/20) and location (Pacific Standard Time/UTC-8), using timeanddate.com
It seems that the problem lies with the sunrise-sunset API, as it produces times for sunrise-sunset that are not correct, nor are results for civil twilight. Further adding to my suspicion is that minutes for all results (except Day Length) are equally eleven minutes past the hour. In the scope of things, and given the time that would be needed to root out the problem, perhaps it would be best to put this to rest.
I’m grateful for your help with all this; thanks, mate.
It may be worth documenting a couple of inaccurate results and then contacting sunrise-sunset.org to let let them know. I’m sure they’d want to know that their times were wrong. They can probably fix their service.