360) { $true_long = $true_long - 360; } while ($true_long < 0) { $true_long += 360; } # Then calculate the Sun's right ascension. $right_asc = rad2deg(atan(0.91764 * tan(deg2rad($true_long)))); # And make sure it's in a range from 0 to 360. while ($right_asc > 360) { $right_asc -= 360; } while ($right_asc < 0) { $right_asc += 360; } # Make sure RA is in same quadrant as longitude $long_quad = floor($true_long / 90) * 90; $ra_quad = floor($right_asc / 90) * 90; $right_asc = $right_asc + ($long_quad - $ra_quad); # And convert it to hours. $right_hour = $right_asc / 15; # Now calculate the Sun's declination. $sin_dec = 0.39782 * sin(deg2rad($true_long)); $cos_dec = cos(asin($sin_dec)); # And then the Sun's local hour angle $loc_hr_ang = (cos(deg2rad($zenith)) - ($sin_dec * sin(deg2rad($latitude)))) / ($cos_dec * cos(deg2rad($latitude))); if ($rise_set == 'rise' && $loc_hr_ang > 1) { return 'never'; } if ($rise_set == 'set' && $loc_hr_ang < -1) { return 'never'; } # Calculate time and convert to hours: if ($rise_set == 'rise') { $time = 360 - rad2deg(acos($loc_hr_ang)); } else { $time = rad2deg(acos($loc_hr_ang)); } $time /= 15; # Calculate local mean rise/set time $local_mean = $time + $right_hour - (0.06571 * $approx_time) - 6.622; # adjust back into UTC $time_UTC = $local_mean - $long_hour; # And make sure it's in the 0-24 range while ($time_UTC > 24) { $time_UTC -= 24; } while ($time_UTC < 0) { $time_UTC += 24; } # Finally, convert UTC back to local time zone $local_full = $time_UTC + $local_tz; # And make sure it's in the 0-24 range while ($local_full > 24) { $local_full -= 24; } while ($local_full < 0) { $local_full += 24; } if ($string_or_float != 'float') { $local_hour = floor($local_full); $local_min = $local_full - $local_hour; while ($local_hour > 24) { $local_hour -= 24; } while ($local_hour < 0) { $local_hour += 24; } $local_min = floor($local_min * 60); return($local_hour . ":" . $local_min); } else { return $local_full; } } # -------------------------------------------------------------- function time_of_day($when = 0) { /* Uses the date_sunrise_sunset() function, above, to return the current time of day ("morning", "afternoon", "day", "evening", or "night"), as defined by the Sun's actual movements. Note that this will be valid only for your server's time zone. */ $dawn = date_sunrise_sunset('rise'); $dusk = date_sunrise_sunset('set'); if (! $when) { $when = time(); } $now = date('G', $when) + (date('i', $when) / 60); if ($now < ($dawn - 0.5)) { return 'night'; } elseif ($now >= ($dawn - 0.5) && $now < 10.5) { return 'morning'; } elseif ($now >= 10.5 && $now < 13.5) { return 'day'; } elseif ($now >= 13.5 && $now < ($dusk - 0.5)) { return 'afternoon'; } elseif ($now >= ($dusk - 0.5) && $now < 22.5) { return 'evening'; } else { return 'night'; } } ?>