Free cookie consent management tool by TermsFeed Generator PHP calculate distance between 2 places in 9 easy steps | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP calculate distance between 2 places in 9 easy steps

PHP calculate distance between 2 places in 9 easy steps

Last Updated on Feb 14, 2023

Introduction

For whatever reason you might need to calculate the distance between two place.

In order to calculate the distance between two places we need the latitude and longitude of both of those places.

The formula might look hard but it’s actually very easy and with the help of mathematical functions in php we can easily do that. So let’s get started

Steps

1- we need to calculate the difference of longitude of the second place and the first place

$longDiff = $placeA['longitude'] - $placeB['longitude'];

2- we need to convert this value to radians with deg2rad function in PHP

$longDiffRad = deg2rad($longDiff);

3- and then calculate its cosine

$cosThetaRad = cos($longDiffRad);

4- we need to convert the latitude of both places to radians

Radian is 180 / pi but we don’t need to worry about it. PHP will take care of it.

$latARad = deg2rad($placeA['latitude']);
$latBRad = deg2rad($placeB['latitude']);

5- we need to calculate the sine and cosine of both converted latitudes

$sinLatARad = sin($latARad);
$sinLatBRad = sin($latBRad);
$cosLatARad = cos($latARad);
$cosLatBRad = cos($latBRad);

6- let’s do the magic and calculate the distance

$dist = $sinLatARad * $sinLatBRad + $cosLatARad * $cosLatBRad * $cosThetaRad;

7- now let’s calculate the arc cosine of the distance

 $arcCosDist = acos($dist);

8- and finally let’s multiply that to the radius of the earth

$radius = 6364.963; // kilometers
$arcCosDist * $radius;

9- we are done but we can round the final number as well

round($arcCosDist * $radius,2)

Now let’s see the whole formula at once

$radius = 6364.963; // kilometers
$longDiff = $placeA['longitude'] - $placeB['longitude'];
$longDiffRad = deg2rad($longDiff);
$cosThetaRad = cos($longDiffRad);
$latARad = deg2rad($placeA['latitude']);
$latBRad = deg2rad($placeB['latitude']);
$sinLatARad = sin($latARad);
$sinLatBRad = sin($latBRad);
$cosLatARad = cos($latARad);
$cosLatBRad = cos($latBRad);
$dist = $sinLatARad * $sinLatBRad + $cosLatARad * $cosLatBRad * $cosThetaRad;
$arcCosDist = acos($dist);
echo round($arcCosDist * $radius,2);

We can turn it into a function and test it with different places

$locationA = ['latitude' => 41.0138, 'longitude' => 28.9497]; // istanbul
$locationB = ['latitude' => 52.5244, 'longitude' => 13.4105]; // berlin

function howLong(array $placeA,array $placeB){
   $radius = 6364.963; // kilometers
   $longDiff = $placeA['longitude'] - $placeB['longitude'];
   $longDiffRad = deg2rad($longDiff);
   $cosThetaRad = cos($longDiffRad);
   $latARad = deg2rad($placeA['latitude']);
   $latBRad = deg2rad($placeB['latitude']);
   $sinLatARad = sin($latARad);
   $sinLatBRad = sin($latBRad);
   $cosLatARad = cos($latARad);
   $cosLatBRad = cos($latBRad);
 
   $dist = $sinLatARad * $sinLatBRad + $cosLatARad * $cosLatBRad * $cosThetaRad;
   $arcCosDist = acos($dist);
 
   return round($arcCosDist * $radius, 2);
}

echo howLong($locationA,$locationB);

Conclusion

Now you know about caclulating the distance between two places in PHP.

I recommend you to open a PHP files and try to write the function and try it with different latitude and longitudes.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • calculate distance in php
  • using mathematics in php

Category: programming

Tags: #php #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Loops
Mar 21, 2023 programming

PHP Loops

Today we are going to learn about loops in PHP. Loop is another important concept in programming and we are going to learn everything about loops in PHP. ...

11 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 Min Read Read More
PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications
Jul 17, 2025 programming

PHP Beginner to Advanced 2025: The Complete Guide to Mastering PHP and Building Modern Web Applications

PHP is often called the engine room of the modern web. Despite debates about newer technologies like Node.js, Go, or Python’s Django, PHP has remained a consistent favorite among developers. In fact, as of 2025, PHP powers over 75% of all websites that use a known server-side programming language. ...

9 Min Read Read More
Laravel 12 Beginner to Advanced: Complete Guide
Jul 17, 2025 programming

Laravel 12 Beginner to Advanced: Complete Guide

Laravel is a name that resonates throughout the PHP community. With its elegant syntax, robust ecosystem, and developer-friendly tools, Laravel has become the go-to framework for modern web development. Laravel 12, released on February 24, 2025, continues this tradition, streamlining workflows and boosting performance with support for the latest PHP advancements. ...

20 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...