PHPackages                             lounisbou/php-distance - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lounisbou/php-distance

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lounisbou/php-distance
======================

A PHP library for calculating the distance between two geographical points on Earth using various Earth radius values.

v1.0.0(1y ago)191MITPHPPHP ^8.1

Since Sep 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/LounisBou/php-distance)[ Packagist](https://packagist.org/packages/lounisbou/php-distance)[ RSS](/packages/lounisbou-php-distance/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (1)

php-distance
============

[](#php-distance)

php-distance is a simple PHP library for calculating the distance between two geographical points on Earth. It supports different Earth radius values and various distance calculation formulas.

Installation
------------

[](#installation)

Use Composer to install the library:

```
composer require lounisbou/phpdistance
```

Usage
-----

[](#usage)

Define Points Define geographical points using the `Point` class:

```
use PHPDistance\Point;

$start = new Point(52.5200, 13.4050); // Berlin
$end = new Point(48.8566, 2.3522); // Paris
```

### Calculate Distance Using Haversine Formula

[](#calculate-distance-using-haversine-formula)

Use the `Route` class with the `HaversineCalculator` to calculate the distance using the Haversine formula:

```
use PHPDistance\Route;
use PHPDistance\HaversineCalculator;
use PHPDistance\Enums\EarthRadius;

$start = new Point(52.5200, 13.4050); // Berlin
$end = new Point(48.8566, 2.3522); // Paris
$calculator = new HaversineCalculator(EarthRadius::MEAN_RADIUS);
$route = new Route($start, $end, $calculator);
$distance = Route::getHumanReadableDistance($haversineCalculator->calculate($route));
echo "Distance using Haversine formula with mean radius: " . $distance;
```

### Calculate Distance Using Vincenty Formula

[](#calculate-distance-using-vincenty-formula)

Use the `Route` class with the `VincentyCalculator` to calculate the distance using Vincenty's formula:

```
use PHPDistance\Route;
use PHPDistance\VincentyCalculator;

$start = new Point(52.5200, 13.4050); // Berlin
$end = new Point(48.8566, 2.3522); // Paris
$calculator = new VincentyCalculator();
$route = new Route($start, $end, $calculator);
$distance = Route::getHumanReadableDistance($vincentyCalculator->calculate($route));
echo "Distance using Vincenty formula: " . $distance;
```

Distance Calculation Formulas
-----------------------------

[](#distance-calculation-formulas)

### Haversine Formula

[](#haversine-formula)

The Haversine formula is an equation giving great-circle distances between two points on a sphere from their longitudes and latitudes. It is useful for calculating the shortest distance over the earth's surface.

### Vincenty Formula

[](#vincenty-formula)

Vincenty's formulae are two related iterative methods used in geodesy to calculate the distance between two points on the surface of an ellipsoid. They are accurate for long distances.

### MySQL Formula

[](#mysql-formula)

The MySQL formula is a simplified version of the Haversine formula that can be used in MySQL queries to calculate distances between two points.

### Custom Earth Radius

[](#custom-earth-radius)

You can specify a custom Earth radius if needed:

```
$customRadius = 6356752.3142; // Custom radius in meters
$calculator = new HaversineCalculator($customRadius);
$route = new Route($start, $end, $calculator);

echo "Distance using Haversine formula with custom radius: " . $route->calculateDistance() . " meters\n";
```

Adding More Formulas
--------------------

[](#adding-more-formulas)

You can extend the library by adding more distance calculation formulas. Implement the `DistanceCalculatorInterface` to create a new calculator:

```
use PHPDistance\Point;
use PHPDistance\DistanceCalculatorInterface;

class NewFormulaCalculator implements DistanceCalculatorInterface
{
public function calculate(Point $from, Point $to): int
{
// Implement the new formula here
return 0; // Placeholder
}
}
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request or open an Issue on GitHub.

License
-------

[](#license)

This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

601d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/27da0287b0c5fcbf2addfe0acc6cbca134f4262fa61542deb137810b06fd14f9?d=identicon)[LounisBou](/maintainers/LounisBou)

---

Top Contributors

[![LounisBou](https://avatars.githubusercontent.com/u/34158993?v=4)](https://github.com/LounisBou "LounisBou (18 commits)")

---

Tags

phpgeographicaldistanceearthradius

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lounisbou-php-distance/health.svg)

```
[![Health](https://phpackages.com/badges/lounisbou-php-distance/health.svg)](https://phpackages.com/packages/lounisbou-php-distance)
```

###  Alternatives

[mjaschen/phpgeo

Simple Yet Powerful Geo Library

1.6k8.6M22](/packages/mjaschen-phpgeo)[jeroendesloovere/distance

Get distance between two locations using PHP.

3462.8k](/packages/jeroendesloovere-distance)[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)[fjw/color-compare

A library for converting colors (Hex, RGB, HSL, CIELAB (LAB), DIN-99) and calculating color distances based on DIN-99.

1310.0k](/packages/fjw-color-compare)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
