PHPackages                             zenstruck/dimension - 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. zenstruck/dimension

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

zenstruck/dimension
===================

Wrap quantity and unit of measure with conversions/humanizers.

v1.0.0(5mo ago)51.3k2MITPHPPHP &gt;=8.1CI passing

Since Jul 21Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/zenstruck/dimension)[ Packagist](https://packagist.org/packages/zenstruck/dimension)[ Docs](https://github.com/zenstruck/dimension)[ GitHub Sponsors](https://github.com/kbond)[ GitHub Sponsors](https://github.com/nikophil)[ RSS](/packages/zenstruck-dimension/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

zenstruck/dimension
===================

[](#zenstruckdimension)

[![CI Status](https://github.com/zenstruck/dimension/workflows/CI/badge.svg)](https://github.com/zenstruck/dimension/actions?query=workflow%3ACI)[![codecov](https://camo.githubusercontent.com/17e5a918f058ea5c3718261877200bf84ffe06ca0c7f7e76bd59b322328fbd7e/68747470733a2f2f636f6465636f762e696f2f67682f7a656e73747275636b2f64696d656e73696f6e2f6272616e63682f312e782f67726170682f62616467652e7376673f746f6b656e3d3235354f315141325555)](https://codecov.io/gh/zenstruck/dimension)

Wrap quantity and unit of measure with conversions/humanizers.

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

[](#installation)

```
composer require zenstruck/dimension
```

Usage
-----

[](#usage)

A *dimension* consists of a *quantity* (`numeric`) and a *unit of measure* (`string`).

### Dimension Object

[](#dimension-object)

```
use Zenstruck\Dimension;

// create
$dimension = new Dimension(45.458, 'ft');
$dimension = Dimension::from('45.458ft'); // equivalent to above

$dimension->quantity(); // 45.458
$dimension->unit(); // "ft"

// render
$dimension->toString(); // "45.46 ft" (max 2 decimal places)
(string) $dimension; // equivalent to above

$dimension->toArray(); // [45.458, "ft"]
json_encode($dimension); // '[45.458, "ft"]'

// use your own formatter
vsprintf('%.4f%s', $dimension->toArray()); // "45.4580ft"
```

#### Conversions

[](#conversions)

A dimension object can be converted to alternate units. The following converters are available:

- *Mass*
- *Length*
- *Temperature*
- *Duration* (length of time)
- *Information* (bytes)
- *Propose additional converters via issue/PR*

Use the `convertTo()` method to perform conversions:

```
use Zenstruck\Dimension;

$dimension = Dimension::from('45ft');

$converted = $dimension->convertTo('m'); // Zenstruck\Dimension
$converted->quantity(); // 13.716
$converted->unit(); // "m"
$converted->toString(); // "13.71 m"

$dimension->convertTo('g'); // throws ConversionNotPossible - cannot convert feet to grams
```

#### Comparisons

[](#comparisons)

Several comparison methods are available:

```
use Zenstruck\Dimension;

$dimension = Dimension::from('45ft');

$dimension->isEqualTo('6m'); // false
$dimension->isLargerThan('6m'); // true
$dimension->isLargerThanOrEqualTo('6m'); // true
$dimension->isSmallerThan('6m'); // false
$dimension->isSmallerThanOrEqualTo('6m'); // false
$dimension->isWithin('6m', '1km'); // true
$dimension->isOutside('6m', '1km'); // false
```

#### Mathematical Operations

[](#mathematical-operations)

```
use Zenstruck\Dimension;

$dimension = Dimension::from('45ft');

$dimension->add('6m')->toString(); // "64.69 ft"
$dimension->subtract('6m')->toString(); // "25.31 ft"
```

### Information Object

[](#information-object)

`Zenstruck\Dimension\Information` extends `Zenstruck\Dimension` so it has the [same API](#dimension-object) with some additional features related to humanizing bytes.

```
use Zenstruck\Dimension\Information;

$info = Information::from('4568897B');
$info = Information::from(4568897); // equivalent to above (can create from bytes directly)

$info->bytes(); // 4568897

// "humanize"
(string) $info->humanize(); // "4.57 MB" (defaults to the decimal system)
(string) $info->asBinary()->humanize(); // "4.36 MiB" (convert to binary system before humanizing)

(string) Information::binary(4568897)->humanize(); // "4.36 MiB" (explicitly create in binary system)

// when creating with a unit of measure, the system is detected
(string) Information::from('4570 kb')->humanize(); // "4.57 MB"
(string) Information::from('4570 KiB')->humanize(); // "4.46 MiB"
```

### Duration Object

[](#duration-object)

`Zenstruck\Dimension\Duration` extends `Zenstruck\Dimension` so it has the [same API](#dimension-object) with the ability to *humanize* a duration.

```
use Zenstruck\Dimension\Duration;

$duration = Duration::from('8540 s');
$duration = Duration::from(8540); // equivalent to above (can create from seconds directly)

(string) $duration->humanize(); // "2 hrs"

(string) Duration::from(0)->humanize(); // "0 secs"
(string) Duration::from(1)->humanize(); // "1 sec"
(string) Duration::from(10)->humanize(); // "10 secs"
(string) Duration::from(65)->humanize(); // "1 min"
```

Bridge
------

[](#bridge)

### Twig Extension

[](#twig-extension)

A [Twig](https://twig.symfony.com) extension is provided with `dimension`, `information`, and `duration` filters.

Manual Activation:

```
/* @var Twig\Environment $twig */

$twig->addExtension(new \Zenstruck\Dimension\Bridge\Twig\DimensionExtension());
```

Symfony full-stack activation:

```
# config/packages/zenstruck_dimension.yaml

Zenstruck\Dimension\Bridge\Twig\DimensionExtension: ~

# If not using auto-configuration:
Zenstruck\Dimension\Bridge\Twig\DimensionExtension:
    tag: twig.extension
```

Usage:

```
{{ '45.458ft'|dimension.convertTo('m') }} {# "13.71 m" #}

{{ 4568897|information.humanize() }} {# "4.57 MB" #}

{{ 8540|duration.humanize() }} {# "2 hrs" #}
```

###  Health Score

45

—

FairBetter than 93% of packages

Maintenance78

Regular maintenance activity

Popularity22

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93.8% 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

Every ~652 days

Total

3

Last Release

92d ago

Major Versions

v0.1.0 → v1.0.02025-12-07

PHP version history (2 changes)v0.1.0PHP &gt;=8.0

v1.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/707369cc916e0ea1aacbf077dcba464f611cef879f024d8944311a54a15224b3?d=identicon)[kbond](/maintainers/kbond)

---

Top Contributors

[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (30 commits)")[![Chris53897](https://avatars.githubusercontent.com/u/7104259?v=4)](https://github.com/Chris53897 "Chris53897 (2 commits)")

---

Tags

unitconverthumanizemeasuredimension

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zenstruck-dimension/health.svg)

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

###  Alternatives

[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7825.3M18](/packages/akaunting-laravel-money)[iamcal/php-emoji

This is a PHP library for dealing with Emoji, allowing you to convert between various native formats and displaying them using HTML.

1.3k481.1k](/packages/iamcal-php-emoji)[cartalyst/converter

A framework agnostic measurement conversion and formatting package featuring multiple types of measurements and currency conversion.

88434.4k7](/packages/cartalyst-converter)[gabrielelana/byte-units

Library to parse, format and convert byte units

1672.2M19](/packages/gabrielelana-byte-units)[khill/php-duration

Converts between colon formatted time, human-readable time and seconds

1611.7M20](/packages/khill-php-duration)[misd/linkify

Converts URLs and email addresses in text into HTML links

1122.9M10](/packages/misd-linkify)

PHPackages © 2026

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