Calculate the Distance between the Points in PHP
There is a lot of applications where it's useful to know the distance between the coordinates. Here, you'll find a PHP function that takes the latitude and longitude of the points and returns the distance between them In both miles and metric units.
You can also use this to find the distance between and addresses by taking advantage of the Google geotargetting API.
Here ' s the function:
functionGet_distance_between_points ($latitude 1,$longitude 1,$latitude 2,$longitude 2) {$theta=$longitude 1-$longitude 2;$miles= (Sin(Deg2rad($latitude 1)) *Sin(Deg2rad($latitude 2))) + (Cos(Deg2rad($latitude 1)) *Cos(Deg2rad($latitude 2)) *Cos(Deg2rad($theta)));$miles=ACOs($miles);$miles=rad2deg($miles);$miles=$miles* 60 * 1.1515;$feet=$miles* 5280;$yards=$feet/3;$kilometers=$miles* 1.609344;$meters=$kilometers* 1000;return Compact(' Miles ', ' feet ', ' yards ', ' kilometers ', ' meters '); }
Call
And here's an example of the function in action, using the coordinates in New York city:
$point 1=Array(' lat ' = 40.770623, ' long ' = 73.964367);$point 2=Array(' lat ' = 40.758224, ' long ' = 73.917404);$distance= Get_distance_between_points ($point 1[' Lat '],$point 1[' Long '],$point 2[' Lat '],$point 2[' Long ']);
foreach($distance as $unit=$value) { Echo $unit.‘: ‘.Number_format($value, 4). ' <br/> '; }
The example returns the following:
miles:2.6025//Miles
feet:13,741.4350//FT
yards:4,580.4783//Yards
kilometers:4.1884//km (miles)
meters:4,188.3894//M (m)
from:https://inkplant.com/code/calculate-the-distance-between-two-points.php
[Go] PHP calculates the distance between two coordinates, Calculate the Distance between two Points in PHP