How to Calculate distance between two points in php by passing latitude and longitude

In this quick tutorial I am going to share a quick php function to Calculate distance between two points in php using latitude and longitude,This below function calculates the distance between two points.
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)



Here is the function.

function calDistance($lat1, $lon1, $lat2, $lon2) {
  	$theta = $lon1 - $lon2;
   	$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)));
   	$miles = acos($miles);
   	$miles = rad2deg($miles);
  	$result['miles'] = $miles * 60 * 1.1515;
   	$result['feet'] = $result['miles'] * 5280;
   	$result['yards'] = $result['feet'] / 3;
   	$result['kilometers'] = $result['miles'] * 1.609344;
  	$result['meters'] = $result['kilometers'] * 1000;
  	return $result; 
}

Example.

$distance = calDistance($lat1='28.7041', $lon1='77.1025', $lat2='26.5041', $lon1='75.0025' );
var_dump($distance);



If you like this post please don’t forget to subscribe my public notebook for more useful stuff

Enter your email address to subscribe my public notebook..!!

Join 23,411 other subscribers
Posted in PHP