# 3v4l.org

run code in 200+ php & hhvm versions
```<?php /** *求两个已知经纬度之间的距离,单位为米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离，单位米 *@author www.phpernote.com **/ function getdistance(\$lng1,\$lat1,\$lng2,\$lat2){ //将角度转为狐度 \$radLat1=deg2rad(\$lat1);//deg2rad()函数将角度转换为弧度 \$radLat2=deg2rad(\$lat2); \$radLng1=deg2rad(\$lng1); \$radLng2=deg2rad(\$lng2); \$a=\$radLat1-\$radLat2; \$b=\$radLng1-\$radLng2; \$s=2*asin(sqrt(pow(sin(\$a/2),2)+cos(\$radLat1)*cos(\$radLat2)*pow(sin(\$b/2),2)))*6378.137*1000; return \$s; } echo getdistance(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998); function calculateSignDistance(\$longitude1, \$latitude1, \$longitude2, \$latitude2) { \$lat1 = (\$latitude1 * pi() ) / 180; \$lng1 = (\$longitude1 * pi() ) / 180; \$lat2 = (\$longitude2 * pi() ) / 180; \$lng2 = (\$latitude2 * pi() ) / 180; \$calcLongitude = \$lng2 - \$lng1; \$calcLatitude = \$lat2 - \$lat1; \$stepOne = pow(sin(\$calcLatitude / 2), 2) + cos(\$lat1) * cos(\$lat2) * pow(sin(\$calcLongitude / 2), 2); \$stepTwo = 2 * asin(sqrt(\$stepOne)); return 6378.137*1000 * \$stepTwo; } echo "\n"; echo calculateSignDistance(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998); /** *求两个已知经纬度之间的距离,单位为千米 *@param lng1,lng2 经度 *@param lat1,lat2 纬度 *@return float 距离，单位千米 **/ function distance(\$lng1,\$lat1,\$lng2,\$lat2)//根据经纬度计算距离 { //将角度转为弧度 \$radLat1=deg2rad(\$lat1); \$radLat2=deg2rad(\$lat2); \$radLng1=deg2rad(\$lng1); \$radLng2=deg2rad(\$lng2); \$a=\$radLat1-\$radLat2;//两纬度之差,纬度<90 \$b=\$radLng1-\$radLng2;//两经度之差纬度<180 \$s=2*asin(sqrt(pow(sin(\$a/2),2)+cos(\$radLat1)*cos(\$radLat2)*pow(sin(\$b/2),2)))*6378.137*1000; return \$s; } echo "\n"; echo distance(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998); function getDistanceBetweenPointsNew(\$latitude1, \$longitude1, \$latitude2, \$longitude2) { \$theta = \$longitude1 - \$longitude2; \$miles = (sin(deg2rad(\$latitude1)) * sin(deg2rad(\$latitude2))) + (cos(deg2rad(\$latitude1)) * cos(deg2rad(\$latitude2)) * 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'); } \$point1 = array('lat' => 31.2014966,121, 'long' => 121.40233369999998); \$point2 = array('lat' => 31.22323799999999, 'long' => 121.44552099999998); \$distance = getDistanceBetweenPointsNew(\$point1['lat'], \$point1['long'], \$point2['lat'], \$point2['long']); foreach (\$distance as \$unit => \$value) { echo \$unit.': '.number_format(\$value,4).'<br />'; } /** * @desc 根据两点间的经纬度计算距离 * @param float \$lat 纬度值 * @param float \$lng 经度值 */ function getDistance3(\$lat1, \$lng1, \$lat2, \$lng2) { \$earthRadius = 6378137; //approximate radius of earth in meters /* Convert these degrees to radians to work with the formula */ \$lat1 = (\$lat1 * pi() ) / 180; \$lng1 = (\$lng1 * pi() ) / 180; \$lat2 = (\$lat2 * pi() ) / 180; \$lng2 = (\$lng2 * pi() ) / 180; /* Using the Haversine formula http://en.wikipedia.org/wiki/Haversine_formula calculate the distance */ \$calcLongitude = \$lng2 - \$lng1; \$calcLatitude = \$lat2 - \$lat1; \$stepOne = pow(sin(\$calcLatitude / 2), 2) + cos(\$lat1) * cos(\$lat2) * pow(sin(\$calcLongitude / 2), 2); \$stepTwo = 2 * asin(min(1, sqrt(\$stepOne))); \$calculatedDistance = \$earthRadius * \$stepTwo; return round(\$calculatedDistance); } echo "\n"; echo getDistance3(31.2014966,121.40233369999998,31.22323799999999,121.44552099999998);```
based on 16Mng
Output for 5.6.0 - 7.2.0
4970.4248747391 7081984.7030155 4970.4248747391miles: 2.9612<br />feet: 15,635.0376<br />yards: 5,211.6792<br />kilometers: 4.7656<br />meters: 4,765.5594<br /> 4771