Haversine formula to find distance between two points on a sphere | PHP program for the haversine formula ; distance between latitudes and longitudes ; convert to radians ; apply formulae ; Driver code
< ? php function haversine ( $ lat1 , $ lon1 , $ lat2 , $ lon2 ) { $ dLat = ( $ lat2 - $ lat1 ) * M_PI / 180.0 ; $ dLon = ( $ lon2 - $ lon1 ) * M_PI / 180.0 ; $ lat1 = ( $ lat1 ) * M_PI / 180.0 ; $ lat2 = ( $ lat2 ) * M_PI / 180.0 ; $ a = pow ( sin ( $ dLat / 2 ) , 2 ) + pow ( sin ( $ dLon / 2 ) , 2 ) * cos ( $ lat1 ) * cos ( $ lat2 ) ; $ rad = 6371 ; $ c = 2 * asin ( sqrt ( $ a ) ) ; return $ rad * $ c ; } $ lat1 = 51.5007 ; $ lon1 = 0.1246 ; $ lat2 = 40.6892 ; $ lon2 = 74.0445 ; echo haversine ( $ lat1 , $ lon1 , $ lat2 , $ lon2 ) . " ▁ K . M . " ; ? >