This PHP function calculates the distance between to pairs of latitude longitude coordinates. Returns the distance in miles or kilometers.
function distance($lat1, $lng1, $lat2, $lng2, $miles = true) { $pi80 = M_PI / 180; $lat1 *= $pi80; $lng1 *= $pi80; $lat2 *= $pi80; $lng2 *= $pi80; $r = 6372.797; // mean radius of Earth in km $dlat = $lat2 - $lat1; $dlng = $lng2 - $lng1; $km = $r * $c; return ($miles ? ($km * 0.621371192) : $km); }
Comments
Subscribe to comments
You need to login to post a comment.

great work...tested it with my zipcode database and worked great
Thanks for the function. Was very helpful on a number of my geographic sites to show the distance of nearby points (see example)
Thanks! Great job!
Thanks, this helped me
Awesome, you saved me a lot of time :)
Thanks a lot! very good work.
Great work! Unfortunately didn't match my query results:
SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM members HAVING distance