PHPackages                             zenstruck/bytes - 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/bytes

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

zenstruck/bytes
===============

Parse, manipulate, humanize, and format bytes.

v1.1.0(5mo ago)25662.7k↓14.9%35MITPHPPHP &gt;=8.1CI passing

Since Jul 4Pushed 3mo ago1 watchersCompare

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

READMEChangelog (4)Dependencies (4)Versions (5)Used By (5)

zenstruck/bytes
===============

[](#zenstruckbytes)

[![CI](https://github.com/zenstruck/bytes/actions/workflows/ci.yml/badge.svg)](https://github.com/zenstruck/bytes/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/e525e3b92cd3086a6e7132df279d8c35e4c614a287d542e85557070fbeba75e1/68747470733a2f2f636f6465636f762e696f2f67682f7a656e73747275636b2f62797465732f6272616e63682f312e782f67726170682f62616467652e7376673f746f6b656e3d336a564b4d6567487044)](https://codecov.io/gh/zenstruck/bytes)

A value object to parse, manipulate, humanize, and format bytes.

Installation
============

[](#installation)

```
composer require zenstruck/bytes
```

Usage
=====

[](#usage)

Create a `Zenstruck\Bytes` object and access its value:

```
use Zenstruck\Bytes;

$bytes = Bytes::parse(1024); // create from number of bytes as integer
$bytes->value(); // (int) 1024

$bytes = Bytes::parse('1.54kb'); // create from a quantity suffixed by a valid informational unit
$bytes->value(); // (int) 1540

$bytes = Bytes::parse('1.54 KiB'); // can use binary informational units
$bytes->value(); // (int) 1577
```

Formatting
----------

[](#formatting)

By default, when a `Zenstruck\Bytes` object is converted to a string, it *humanizes* the to make it easier to read.

```
use Zenstruck\Bytes;

(string) Bytes::parse(389789364783); // "389.79 GB"
```

You can customize the formatting:

```
use Zenstruck\Bytes;

Bytes::parse(389789364783)->format('%.4f%s'); // "389.7894GB"
```

### Unit Conversion

[](#unit-conversion)

If you'd like a consistent unit to be used when formatting, use the `to()` method with a valid informational unit:

```
use Zenstruck\Bytes;

(string) Bytes::parse(389789364783)->to('mib'); // "371732.11 MiB"
```

Arithmetic
----------

[](#arithmetic)

Perform *add* and *subtract* operations:

```
use Zenstruck\Bytes;

/** @var Bytes $first */
/** @var Bytes $second */
/** @var Bytes $third */

$result = $first // $result instanceof Bytes
    ->add($second) // add another Bytes object
    ->add(500) // add specific amount of bytes
    ->add('2.1 MB') // parse and add
    ->subtract($third) // subtract another Bytes object
    ->subtract(100) // subtract specific amount of bytes
    ->subtract('100 kib') // parse and subtract
;
```

> **Note**: These operations are immutable.

Comparisons
-----------

[](#comparisons)

You can compare `Zenstruck\Byte` objects:

```
use Zenstruck\Bytes;

/** @var Bytes $bytes */
/** @var Bytes $another */

$bytes->isEqualTo(100); // bool
$bytes->isEqualTo('1.1kb'); // bool
$bytes->isEqualTo($another); // bool

$bytes->isGreaterThan(100); // bool
$bytes->isGreaterThan('1.1kb'); // bool
$bytes->isGreaterThan($another); // bool

$bytes->isLessThan(100); // bool
$bytes->isLessThan('1.1kb'); // bool
$bytes->isLessThan($another); // bool

$bytes->isGreaterThanOrEqualTo(100); // bool
$bytes->isGreaterThanOrEqualTo('1.1kb'); // bool
$bytes->isGreaterThanOrEqualTo($another); // bool

$bytes->isLessThanOrEqualTo(100); // bool
$bytes->isLessThanOrEqualTo('1.1kb'); // bool
$bytes->isLessThanOrEqualTo($another); // bool
```

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance78

Regular maintenance activity

Popularity48

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity56

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

Every ~239 days

Total

5

Last Release

92d ago

Major Versions

v0.2.0 → v1.0.02023-07-26

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

v1.1.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 (11 commits)")

---

Tags

unitinformation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[php-unit-conversion/php-unit-conversion

A fully PSR-4 compatible PHP library for converting between standard units of measure.

681.2M1](/packages/php-unit-conversion-php-unit-conversion)[cartalyst/converter

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

88434.4k7](/packages/cartalyst-converter)[nmarfurt/measurements

A PHP library for representing and converting dimensional units of measure.

17170.2k1](/packages/nmarfurt-measurements)[isolate/unit-of-work

Unit of Work

2323.1k5](/packages/isolate-unit-of-work)

PHPackages © 2026

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