I was using PHP to calculate sunrise and sunset using the built-in functions date_sunrise and date_sunset. But initially, the resulting time was ignoring British Summer Times (BST). The solution was to specify whether daylight saving was in force.
First published Jun 19, 2014. Updated 26 Apr 2022.
<?php // Credits // http://blog.stratus.org.uk/?p=695 // https://stackoverflow.com/questions/2763935/how-can-i-detect-day-light-savings-automatically-in-php $gDay = "Friday"; $gLat = "53.146"; $gLongs = "-2.367"; // negative is west of the Greenwich Meridian // GMT or BST? $date = new DateTime('now', new DateTimeZone('Europe/London')); if ($date->format('I')) { $iOffset = 1; // Daylight saving set == BST $sSavings = "BST"; } else { $iOffset = 0; // GMT $sSavings = "GMT"; } // compute sunset and rise using $gDay ... as this changes the further out in days you go if (($iTime = strtotime("next ".$gDay)) === false) { $sSunset = date_sunset(time(), SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset); $sSunrise = date_sunrise(time(), SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset); } else { $sSunset = date_sunset($iTime, SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset); $sSunrise = date_sunrise($iTime, SUNFUNCS_RET_STRING, $gLat,$gLongs, 90, $iOffset); } $sTailName .= "🌝 Sunrise: $sSunrise<br> 🌙 Sunset: $sSunset<br> ($sSavings)"; echo $sTailName; ?>
- Credit:
- The PHP function date(“I”) indicates whether Daylight Saving Time (British Summer Time) is active.
- “Compute Sunrise and Sunset Taking Into Account Daylight Savings in PHP“
- “How can I detect day light savings automatically in PHP?“