Code Sample: PHP

This method calculates sunrise and sunset times for my scenic drives application. Sunrise and sunset times can be calculated knowing just latitude, longitude, date, and elevation.

1<?php

2function get_sunrise_sunset($latitude, $longitude, $timezone, $elevation=null) {

3    try {

4        $timezone = new DateTimeZone($timezone);

5    } catch (Exception $e) {

6        $timezone = new DateTimeZone('UTC');

7    }

8    

9    if ($elevation === null || $elevation == '' || $elevation < 0) $elevation = 0;

10    

11    $dt = new DateTime(null, $timezone);

12    $month = $dt->format('n');

13    $day = $dt->format('j');

14    $year = $dt->format('Y');

15    $day_of_year = $dt->format('z');

16    $utc_offset = $timezone->getOffset($dt)/3600;

17    

18    $longitude_west = -$longitude;

19    $julian_date = juliantojd($month, $day, $year);

20    $julian_day = $julian_date - 2451545 + 0.0008;

21    

22    $mean_solar_noon = $longitude_west / 360 + $julian_day;

23    $solar_mean_anomaly = (357.5291 + (0.98560028 * $mean_solar_noon))% 360;

24    

25    # Equation of Center

26    $center = 1.9148 * sin(deg2rad($solar_mean_anomaly)) + 0.002 * sin(deg2rad(2*$solar_mean_anomaly)) + 0.0003 * sin(deg2rad(3*$solar_mean_anomaly));

27    

28    # Ecliptic Longitude

29    $lambda = ($solar_mean_anomaly + $center + 180 + 102.9372) % 360;

30    

31    # Solar Transit (Solar Noon)

32    $solar_noon = $mean_solar_noon + 0.0053 * sin(deg2rad($solar_mean_anomaly)) - 0.0069 * sin(deg2rad(2*$lambda));

33    

34    # Declination of Sun

35    $declination_angle = -23.44 * cos(deg2rad(360/365 * ($day_of_year + 10)));

36    //$declination_angle = rad2deg(asin(sin(deg2rad($lambda)) * sin(deg2rad(23.44))));

37    

38    # Hour Angle

39    $elevation_correction = -1.15 * sqrt($elevation)/60;

40    $hour_angle = rad2deg(acos((sin(deg2rad(-0.83 + $elevation_correction)) - (sin(deg2rad($latitude)) * sin(deg2rad($declination_angle))))/(cos(deg2rad($latitude)) * cos(deg2rad($declination_angle)))));

41    

42    # Sunset/Sunrise (note these are in UTC)

43    $raw_sunset = $solar_noon + ($hour_angle/360);

44    $raw_sunrise = $solar_noon - ($hour_angle/360);

45    

46    $new_sunrise = (($raw_sunrise - $julian_day) * 24) + 12 + $utc_offset;

47    $new_sunrise_hour = floor($new_sunrise);

48    $new_sunrise_minute = round(($new_sunrise - $new_sunrise_hour) * 60);

49    $sunrise = new DateTime(null, $timezone);

50    $sunrise->setTime($new_sunrise_hour, $new_sunrise_minute);

51    $sunrise_text = $sunrise->format('g:i A T');

52    

53    $new_sunset = (($raw_sunset - $julian_day) * 24) + 12 + $utc_offset;

54    $new_sunset_hour = floor($new_sunset);

55    $new_sunset_minute = round(($new_sunset - $new_sunset_hour) * 60);

56    $sunset = new DateTime(null, $timezone);

57    $sunset->setTime($new_sunset_hour, $new_sunset_minute);

58    $sunset_text = $sunset->format('g:i A T');

59    

60    return array('sunrise' => $sunrise_text, 'sunset' => $sunset_text);

61}

62

63?>