
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?PHP // return sunset date/time $daySunSet = date_sunset(strtotime(date('Y-m-d')), SUNFUNCS_RET_STRING, 40.8626305, -108.7784545, 90.5,-5); // transform sunset time to timestamp seconds $daySunSetSecs = strtotime($daySunSet); // number of seconds in 1 hour $oneHourSecs = 3600; // math for projected start and end times to subtract and add to sunset time in timestamp/seconds $startShift = $oneHourSecs*.9; $endShift = $oneHourSecs*1.15; // projected start time of event $daySunSetStart = $daySunSetSecs-$startShift; // projected end time of event $daySunSetEnd = $daySunSetSecs+$endShift; ?> |
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 sunrise-sunset.org.
Although you’ll need to show a link of attribution to the sunrise-sunset.org website on your website should you use it, the following code should get your started and going in the right direction:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php $url = "https://api.sunrise-sunset.org/json?lat=50.7216&lng=79.2547&date=today"; //open connection $ch = curl_init(); $timeout=60; //set the url and other options for curl curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); //execute post and return response data. $result = curl_exec($ch); //close curl connection curl_close($ch); // decode the json response $dn = json_decode($result, true); echo '<pre>'; print_r($dn); echo '</pre>'; ?> |
There are quite a few api examples on the sunrise-sunset.org website. Whether you choose PHP’s method, or you’re using another programming language and opt to use the sunrise-sunset.org 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.
Leave a Comment