PHPackages                             rmasters/units - 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. rmasters/units

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

rmasters/units
==============

Converts units using a graph of conversions

4472PHP

Since Aug 30Pushed 12y ago1 watchersCompare

[ Source](https://github.com/rmasters/units)[ Packagist](https://packagist.org/packages/rmasters/units)[ RSS](/packages/rmasters-units/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (2)Used By (0)

Units [![Latest Stable Version](https://camo.githubusercontent.com/f4585f3e11880e4b891d271a4d6c44f4f0f9e2bed1e7dc6ae5bc6a636721b734/68747470733a2f2f706f7365722e707567782e6f72672f726d6173746572732f756e6974732f762f737461626c652e706e67)](https://packagist.org/packages/rmasters/units) [![master](https://camo.githubusercontent.com/d71ec38f001eb0b3936eaf39c7eadd7ef748ea187a8d98c356c662d952ecde07/68747470733a2f2f7472617669732d63692e6f72672f726d6173746572732f756e6974732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/rmasters/units) [![Coverage Status](https://camo.githubusercontent.com/1d940765fff46e31a438766af85941939ec2e45c864c34cb0d804ac48b3a15e4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f726d6173746572732f756e6974732f62616467652e706e67)](https://coveralls.io/r/rmasters/units) [![Dependency status](https://camo.githubusercontent.com/9b052967d52ef3e0d03d6c58b0910d5569af361d6029745c96914251b01a7269/687474703a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3532316434333038363332626163373264393033356339332f62616467652e706e67)](http://www.versioneye.com/user/projects/521d4308632bac72d9035c93)
========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#units----)

Units is a small unit-of-measure conversion library written in PHP. It aims to be simple to use and extend.

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

[](#installation)

Units is installable via Composer. Add the following line to your project's composer.json under the `require` section, and run `composer update`.

```
"rmasters/units": "dev-master"

```

Installable revisions of the package are listed at [Packagist](http://packagist.org/packages/rmasters/units) and documented on the [releases page](https://github.com/rmasters/units/releases).

Usage
-----

[](#usage)

The conversion class can either be instantiated directly, or used as a singleton using the supplied facade class. Each Convert instance has its own registry of units and conversions.

Once units and conversions have been registered (see below), values can be converted as so:

```
use Units\Facade as Units;

Units::convert('kg', 'lb', 42); // => 92.568

```

A number of standard conversions are supplied with the library. These functions register with the Facade singleton instance by default, or with a Convert instance if given.

```
// Defined in src/Units/conversions/
Units\register_weights(); // Metric and imperial weights
Units\register_distances(); // Metric and imperial distances

// Registering with a specific Convert instance
$convert = new Convert;
Units\register_distances($convert);

```

Extending with additional conversions/units
-------------------------------------------

[](#extending-with-additional-conversionsunits)

Units uses a graph model for converting between different units. For example, the edges (connections) in the graph below are defined conversions (`to go from unit A to B, do X`). This makes it possible to convert across a range of units without defining lots of conversions.

```
mg - g - kg
           \
            lb - oz
              \
               st

```

In this graph, to convert from `mg` to `st` is possibly by performing the intermediary conversions to `g`, `kg` and `lb`, without a specific `mg->st` conversion being defined.

To define new units and conversions, use the following code (accessed using the Facade):

```
use \Units\Facade as Units;

// Register new units
Units::register(new Unit('Minute', 'min'));
Units::register(new Unit('Second', 'sec'));

// Record some one way conversions
Units::conversion('min', 'sec', function($min) { return $min * 60; });
Units::conversion('sec', 'min', function($sec) { return $sec / 60; });

// Passing a Unit instance automatically registers it, if not already registered
Units::conversion(new Unit('Hour', 'hr'), 'min', function($hr) { return $hr * 60; });
Units::conversion('min', 'hr', function($min) { return $min / 60; });

Units::convert('min', 'hr', 90); // => 1.5

```

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity43

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/34284?v=4)[Ross Masters](/maintainers/rmasters)[@rmasters](https://github.com/rmasters)

---

Top Contributors

[![rmasters](https://avatars.githubusercontent.com/u/34284?v=4)](https://github.com/rmasters "rmasters (7 commits)")

### Embed Badge

![Health badge](/badges/rmasters-units/health.svg)

```
[![Health](https://phpackages.com/badges/rmasters-units/health.svg)](https://phpackages.com/packages/rmasters-units)
```

###  Alternatives

[prgayman/laravel-zatca

Laravel package a helper to Generate the QR code and signed it for ZATCA E-invoicing

2929.4k1](/packages/prgayman-laravel-zatca)

PHPackages © 2026

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