How to get Sunset and Sunrise using PHP

Hi there! Today’s tutorial is quick and, hopefully, easy. If you’ve ever had a web project requiring when the sun rises or sets, then today’s tutorial is just what the programming doctor ordered. Let’s get started!

Today’s tutorial started really about a year or so for me. It all centered around needing to find a more efficient method to update to websites I developed nearly a decade ago.

Both websites required manual intervention to update evening arrival times when the time changed both in spring and fall. I had *thought* about whether or not I could automate my times based on the sunset time, but never really looked into to it.

Well, I had a few free hours Saturday, and to my surprise, I discovered PHP has both date_sunrise and date_sunset functions that return the sunset and sunrise based on the following arguments passed:

  • timestamp – the timestamp (in seconds) of the day from which the sunrise or sunset will be taken.
  • returnFormat – the returnFormat is how you desire the time to be returned: SUNFUNCS_RET_STRING (e.g., 16:46), SUNFUNCS_RET_DOUBLE (e.g., 16.78243132), and SUNFUNCS_RET_TIMESTAMP (e.g., 1095034606 — timestamp, seconds).
  • latitude — Default is positive value for North, negative value for South.
  • longitude — Default is positive value for East, negative value for West.
  • zenith — the angle between the center of the sun and a line perpendicular to earth’s surface using the following angles: 90 — sunrise/sunset: the point where the sun becomes visible or invisible, 96 — civil twilight: conventionally used to signify the start of dawn or start of dusk, 102 — nautical twilight: the point at which the horizon starts being visible at sea or invisible at sea, and 108 — astronomical twilight: the point at which the sun starts or ends being the source of any illumination.
  • utcOffest – value specified using GMT hours

While both functions are useful, the one I needed to concern myself with was date_sunset. I was able to use date_sunset function to determine the projected start and end times based on daily sunset. Here’s the line of code I used (using example latitude and longitude):

In my example, you can see I used 90.5 for the zenith argument. You can tweak the value, including using decimals, based on the location if it’s truly off. What actually helped me most though was another website that also provides an API for sunrise and sunset, appropriately named

Although you’ll need to show a link of attribution to the website on your website should you use it, the following code should get your started and going in the right direction:

There are quite a few api examples on the website. Whether you choose PHP’s method, or you’re using another programming language and opt to use the API, both are quite powerful and easy to integrate.

I hope this tutorial helps you as it did me. Let me know if you encounter and technical challenges, and I’ll do my best to assist you.

Written by Alvin Brown
He's an experienced and passionate serial entrepreneur, founder and publisher of Kickstart Commerce. Alvin possesses a great love for startups dominating their market using profitable digital strategies for greater commerce.