Skip to content

Instantly share code, notes, and snippets.

@PatricNox
Created September 3, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatricNox/1318e1814efecaece3e74038bf7ad80f to your computer and use it in GitHub Desktop.
Save PatricNox/1318e1814efecaece3e74038bf7ad80f to your computer and use it in GitHub Desktop.
PHP - Get the sunrise/sunset time with accuracy
<?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