Skip to main content


Showing posts from October, 2022

How calculate the distance between two latitudes and longitudes using the Haversine formula

The Haversine formula is a formula used to calculate the great-circle distance (the shortest distance on a sphere between two points) between two points, usually specified in terms of their latitudes and longitudes. It is commonly used in navigation and is named after the haversine function, which is used in its calculation. The Haversine formula is: d = 2r arcsin(sqrt(sin²((lat₂ - lat₁)/2) + cos(lat₁) * cos(lat₂) * sin²((lon₂ - lon₁)/2))) where: d is the distance between the two points in the same units as the radius r lat₁, lon₁ are the latitude and longitude, respectively, of the first point in radians lat₂, lon₂ are the latitude and longitude, respectively, of the second point in radians r is the radius of the sphere Note that this formula assumes that the Earth is a perfect sphere, which is not exactly true but is a close enough approximation for many practical purposes. In PHP, the Haversine formula can be implemented using the sin , cos , sqrt , asin , deg2rad , and rad2deg fun