Skip to content

Instantly share code, notes, and snippets.

@Sanaldev
Last active February 4, 2016 09:14
Show Gist options
  • Save Sanaldev/7480347 to your computer and use it in GitHub Desktop.
Save Sanaldev/7480347 to your computer and use it in GitHub Desktop.
Haversine formula in PHP
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371000;
$dLat = deg2rad($latitude2 - $latitude1);
$dLon = deg2rad($longitude2 - $longitude1);
$a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);
$c = 2 * asin(sqrt($a));
$d = $earth_radius * $c;
return $d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment