Created
September 3, 2019 09:34
-
-
Save PatricNox/1318e1814efecaece3e74038bf7ad80f to your computer and use it in GitHub Desktop.
PHP - Get the sunrise/sunset time with accuracy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Calculate the sunrise/sunset time for Gothenburg, Sweden | |
* | |
* Latitude: 57.696991 | |
* Longitude: 11.986500) | |
*/ | |
$date_sun_info = date_sun_info(strtotime("2019-09-03"), 57.696991, 11.986500); | |
$sun_info = array( | |
'sunrise' => sunrise_sunset_gmt($date_sun_info['sunrise']), | |
'sunset' => sunrise_sunset_gmt($date_sun_info['sunset']), | |
); | |
function sunrise_sunset_gmt($sun_type) | |
{ | |
// snap to UTC. | |
$date = new DateTime("@".$sun_type); | |
// Retrieve GMT, Timezone through country. | |
$date->setTimezone(new DateTimeZone('Europe/Stockholm')); | |
// Sweden format. | |
return $date->format('H:i'); | |
} | |
// Just for show. | |
// (php index.php) in cmd | |
echo "___________"; | |
echo PHP_EOL; | |
echo " Sunrise: " . $sun_info['sunrise'] . PHP_EOL; | |
echo " Sunset: " . $sun_info['sunset'] . PHP_EOL; | |
echo "___________"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment