Quick Search:

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

```<?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 = ACOS(\$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>";

?>```