datetime - Calculating Summer/Winter Solstice in PHP? -
php allows me check sunrise , sunset times day @ specific latitude & longitude.
is there simple way calculate day solstice? mean - @ specific location, day has hours of sunlight , has least?
i wouldn't call "simple", thought calculating time differences between sunrise , sunset in each day, storing data in array, , finding min/max value. iv'e made quick, hope useful:
(i used random long/lat)
function solstice() { // set timezone date_default_timezone_set('utc'); $date='2014/01/01'; $end_date='2014/12/31'; $i = 0; //loop through year while(strtotime($date)<=strtotime($end_date)) { $sunrise=date_sunrise(strtotime($date),sunfuncs_ret_double,31.47,35.13,90,3); $sunset=date_sunset(strtotime($date),sunfuncs_ret_double,31.47,35.13,90,3); //calculate time difference $delta = $sunset-$sunrise; //store time difference $delta_array[$i] = $delta; //store date $dates_array[$i] = $date; $i++; //next day $date=date("y-m-d",strtotime("+1 day",strtotime($date))); } $shortest_key = array_search(min($delta_array), $delta_array); $longest_key = array_search(max($delta_array), $delta_array); echo "the longest day is:".$dates_array[$longest_key]. "<br />"; echo "the shortest day is:".$dates_array[$shortest_key]. "<br />"; }
Comments
Post a Comment