# PHP Get the Distance Between Two Lat/Lng Points If your building some custom Google Maps and need to get the distance between two points I got the Math done for ya.

```<?php
/**
* Return a distance between two lat/lng points
*
* @param int \$lat1         REQUIRED; Latitude - Location 1
* @param int \$lon1         REQUIRED; Longitude - Location 1
* @param int \$lat2         REQUIRED; Latitude - Location 2
* @param int \$lon2         REQUIRED; Longitude - Location 2
* @param string \$unit      OPTIONAL; Distance Type (Km / M)
* @return int
*/
function getDistance(\$lat1, \$lon1, \$lat2, \$lon2, \$unit) {
if(\$lat1 == \$lat2 && \$lon1 == \$lon2)
return 0;

\$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;
}
}
?>
```

The function below will return the distance between two Lat/Lng points so for you to use in your code to calculate travel fees or just the distance in miles/kilometers. This is a very handy bit of code.