Plans | Login    

Briefing Search
Keyword:
Category:

       

    
Author Details
andrey prikaznov
scripttutorials.com@gmail.com
www.script-tutorials.com/

Useful PHP Snippets tutorials


PHP is the most widely used language when it comes to server-side programming. If you are a beginner or an advanced programmer and you use it in your work – our article will be very useful for you.

BriefingWire.com, 12/04/2013 - seful PHP Snippets

PHP is the most widely used language when it comes to server-side programming. If you are a beginner or an advanced programmer and you use it in your work – our article will be very useful for you. Because today I want to bring to your attention a small collection of quite interesting and useful php snippets. They are designed as finished functions, and you can easily transfer them in a class or a library file for later use in your projects.

Calculate the distance between two coordinates

When we need to measure the distance between two points, we may use one of the following formulas: Haversine formula or Vincenty’s formula. There are two appropriately named functions:

view sourceprint?

01

function haversineGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) {

02

// convert from degrees to radians

03

$latFrom = deg2rad($latitudeFrom);

04

$lonFrom = deg2rad($longitudeFrom);

05

$latTo = deg2rad($latitudeTo);

06

$lonTo = deg2rad($longitudeTo);

07

08

$latDelta = $latTo - $latFrom;

09

$lonDelta = $lonTo - $lonFrom;

10

11

$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +

12

cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));

13

return $angle * $earthRadius;

14

}

15

16

public static function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000) {

17

// convert from degrees to radians

18

$latFrom = deg2rad($latitudeFrom);

19

$lonFrom = deg2rad($longitudeFrom);

20

$latTo = deg2rad($latitudeTo);

21

$lonTo = deg2rad($longitudeTo);

22

23

$lonDelta = $lonTo - $lonFrom;

24

$a = pow(cos($latTo) * sin($lonDelta), 2) +

25

pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);

26

$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

27

28

$angle = atan2(sqrt($a), $b);

29

return $angle * $earthRadius;

30

}

Both functions use the following parameters:

float $latitudeFrom – Latitude of start point in [deg decimal]

float $longitudeFrom – Longitude of start point in [deg decimal]

float $latitudeTo – Latitude of target point in [deg decimal]

float $longitudeTo – Longitude of target point in [deg decimal]

float $earthRadius – Mean earth radius in miles

Functions return – (float) Distance between points in miles (same as earthRadius)

 
 
FAQs | Contact Us | Terms & Conditions | Privacy Policy
© 2026 Proserve Technology, Inc.