Skip to content

Instantly share code, notes, and snippets.

@dracos
Last active December 25, 2025 10:21
Show Gist options
  • Select an option

  • Save dracos/799c6e74fcb30543bfcc38b9500d6adf to your computer and use it in GitHub Desktop.

Select an option

Save dracos/799c6e74fcb30543bfcc38b9500d6adf to your computer and use it in GitHub Desktop.
Sunlight optimism core loop - https://dracos.co.uk/made/sunlight-optimism/
# Have a $lat and a $lon when you get here
$time = time();
$nextyear = strtotime('21 June');
if ($nextyear < $time) $nextyear += 365*86400;
$row = [];
while ($time < $nextyear) {
$last = date_sun_info($time-86400, $lat, $lon);
$sun = date_sun_info($time, $lat, $lon);
$rise = $sun['sunrise'];
$set = $sun['sunset'];
$Hrise = date('H', $rise);
$Hset = date('H', $set);
$Lrise = $last['sunrise'];
$Lset = $last['sunset'];
$HLrise = date('H', $Lrise);
$HLset = date('H', $Lset);
for ($h=8; $h<=24; $h++) {
if ($set-$rise >= $h*3600 && $Lset-$Lrise < $h*3600) {
$row[] = [$time, $rise, $set, 'day', $h];
}
}
for ($r=0; $r<=11; $r++) {
if ($Hrise < $r && $HLrise == $r) {
$row[] = [$time, $rise, $set, 'rise', $r];
}
}
for ($r=13; $r<=23; $r++) {
if ($Hset == $r && $HLset < $r) {
$row[] = [$time, $rise, $set, 'set', $r-12];
}
}
$time += 86400;
}
print_r($row); # Display how you like
$t=time();$n=strtotime('6/21');if($n<$t)$n+=31536E3;$o=[];while ($t<$n){$l=date_sun_info($t-86400,$lat,$lon);$u=date_sun_info($t,$lat,$lon);$r=$u['sunrise'];$s=$u['sunset'];$lr=$l['sunrise'];$ls=$l['sunset'];for ($h=7;++$h<25;){if($s-$r>=$h*3600&&$ls-$lr<$h*3600){$o[]=[$t,$r,$s,'day',$h];}}for ($h=-1;++$h<12;){if (date('H',$r)<$h&&date('H',$lr)==$h){$o[]=[$t,$r,$s,'rise',$h];}}for ($h=12;++$h<24;){if (date('H',$s)==$h&&date('H',$ls)<$h){$o[]=[$t,$r,$s,'set',$h-12];}}$t+=86400;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment