Distance calculation using latitude and longitude in PHP

Here is the simple distance calculation formula to get distance between two places.

Using this function you can get distance between two places in Kilo Meter [KM]

In the following distance calculation function, We have use the haversine formula to calculate the distance between two points,
feeding it the latitude and longitude of both points.

/*
Description: Distance calculation from the latitude/longitude of 2 points
Author: Massive Technolab
Website: http://massivetechnolab.co.in
 
UNALTERED, this code is licensed under the GNU General Public License version 3:
http://www.gnu.org/licenses/gpl.html
*/

function getDistanceBetweenToPlace($lat1,$long1,$lat2,$long2){
$distance = 3956 * 2 * ASIN(SQRT(pow(SIN(($lat1 - $lat2) * pi() / 180 / 2), 2) + COS($lat1 * pi() / 180) * COS($lat2 * pi() / 180) * pow(SIN(($long1 - $long2) * pi() / 180 / 2), 2)));
$distance = 2 * (round($distance));
return $distance. "km";
}

Example

  "23.0118", "long" => "72.5063"); // prahalad nagar garden, prahlad nagar, ahmedabad, gujarat 380015
$point2 = array("lat" => "23.0329", "long" => "72.6284"); // bapunagar, ahmedabad, gujarat, india
$km = GetDistanceBetweenToPlace($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); // Calculate distance in kilometres
echo "The distance between prahalad nagar garden (India) and bapunagar City (India) is $km ";
?>

About The Massive Technolab

Avatar
Who We Are

Massive Technolab is a Leading IT services Company in India that expertise in web Development, Web Design and SEO at affordable rates.

Leave a Comment

comments powered by Disqus