Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 PHP Code: Calculate Zipcode Distances Jump to:  
Category: >> PHP Code >> Calculate Zipcode Distances Bookmark and Share

<< lastnext >>

Snippet Name: Calculate Zipcode Distances

Description: This routine calculates the distance between two points (given the
latitude/longitude of those points). it is being used to calculate
the distance between two zip codes or postal codes.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: February 26th, 2009

<?PHP
 
/*::                                                                         :*/
/*::  definitions:                                                           :*/
/*::    south latitudes are negative, east longitudes are positive           :*/
/*::                                                                         :*/
/*::  passed to function:                                                    :*/
/*::    lat1, lon1 = latitude and longitude of point 1 (in decimal degrees)  :*/
/*::    lat2, lon2 = latitude and longitude of point 2 (in decimal degrees)  :*/
/*::    unit = the unit you desire for results                               :*/
/*::           where: 'm' is statute miles                                   :*/
/*::                  'k' is kilometers (default)                            :*/
/*::                  'n' is nautical miles                                  :*/
 
FUNCTION distance($lat1, $lon1, $lat2, $lon2, $unit) { 
 
  $theta = $lon1 - $lon2; 
  $dist = SIN(DEG2RAD($lat1)) * SIN(DEG2RAD($lat2)) +  COS(DEG2RAD($lat1)) * COS(DEG2RAD($lat2)) * COS(DEG2RAD($theta)); 
  $dist = ACOS($dist); 
  $dist = RAD2DEG($dist); 
  $miles = $dist * 60 * 1.1515;
  $unit = STRTOUPPER($unit);
 
  IF ($unit == "K") {
    RETURN ($miles * 1.609344); 
  } ELSE IF ($unit == "N") {
      RETURN ($miles * 0.8684);
    } ELSE {
        RETURN $miles;
      }
}
 
ECHO distance(32.9697, -96.80322, 29.46786, -98.53506, "m") . " miles<br>";
ECHO distance(32.9697, -96.80322, 29.46786, -98.53506, "k") . " kilometers<br>";
ECHO distance(32.9697, -96.80322, 29.46786, -98.53506, "n") . " nautical miles<br>";
 
?>


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 126 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?