How to calculate distances in PHP

Here is a very handy function, which calculate the distance from a point A to a point B, using latitudes and longitudes. The function can return the distance in miles, kilometers, or nautical miles.

[php][file]http://w3lessons.info/wp-content/uploads/2011/12/calculate_distance1.txt[/file][/php]

Usage

Using the function is pretty simple: All you need to do is to call it. Parameters are: from latitude, fromlongitude, to latitude, to longitude and unit, which can be m for miles, k for kilometers, and n for nautical miles

//Miles echo calculate_distance(32.9697, -96.80322, 29.46786, -98.53506, “m”) . ” miles”;

//Kilometers echo calculate_distance(32.9697, -96.80322, 29.46786, -98.53506, “k”) . ” kilometers”;

//Nautical miles echo calculate_distance(32.9697, -96.80322, 29.46786, -98.53506, “n”) . ” nautical miles”;

You May Also Like

Never Miss Any Web Tutorials, Guides, Tips and Free eBooks

Join Our Community Of 50,000+ Web Lovers and get a weekly newsletter in your inbox

 

I hate spam too. Unsubscribe at any time.

2 thoughts on “How to calculate distances in PHP”

Leave a Comment