PHPackages                             bermudaphp/byte - 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. bermudaphp/byte

ActiveLibrary

bermudaphp/byte
===============

A robust PHP class for working with data size units (bytes, kilobytes, megabytes, etc.)

v2.0(1y ago)1351MITPHPPHP ^8.4

Since Mar 26Pushed 1y agoCompare

[ Source](https://github.com/bermudaphp/byte)[ Packagist](https://packagist.org/packages/bermudaphp/byte)[ RSS](/packages/bermudaphp-byte/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (6)Used By (1)

Byte and BitRate Classes
========================

[](#byte-and-bitrate-classes)

[![PHP Version](https://camo.githubusercontent.com/9c2f8ad80d34105266a94c4c06234f8ed18c968d3595039c2d9a7becd1e71c8b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e342d626c75652e737667)](https://www.php.net/)[![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](https://opensource.org/licenses/MIT)[![Ask DeepWiki](https://camo.githubusercontent.com/0f5ae213ac378635adeb5d7f13cef055ad2f7d9a47b36de7b1c67dbe09f609ca/68747470733a2f2f6465657077696b692e636f6d2f62616467652e737667)](https://deepwiki.com/bermudaphp/byte)

*Read this in other languages: [Russian](README.ru.md)*

Overview
--------

[](#overview)

The library provides PHP classes for working with data sizes (Byte) and data transfer rates (BitRate) with comprehensive functionality for conversion, comparison, arithmetic operations, and human-readable formatting.

Features
--------

[](#features)

- **Unit Conversion**: Easily convert between different data units (B, kB, MB, GB, TB, PB, EB, ZB, YB)
- **Transfer Rate Handling**: Work with bit rates in various units (bps, kbps, Mbps, Gbps, etc.)
- **Arithmetic Operations**: Perform addition, subtraction, multiplication, division, and modulo operations
- **Comparison**: Compare values with support for both individual and array-based comparisons
- **Formatting**: Format values as human-readable strings with multi-language support
- **Range Operations**: Create ranges of values, find min/max, calculate averages
- **Transfer Time Calculation**: Estimate data transfer times based on bandwidth
- **Internationalization**: Format time durations in multiple languages

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

[](#installation)

You can install the package via composer:

```
composer require bermudaphp/byte
```

Usage
-----

[](#usage)

### Byte Class

[](#byte-class)

#### Creating Byte Instances

[](#creating-byte-instances)

There are multiple ways to create a `Byte` instance:

```
use Bermuda\Stdlib\Byte;

// From numeric value (bytes)
$bytes = new Byte(1024);

// From string
$bytes = new Byte('1024 kB');

// Using static factory methods
$bytes = Byte::new(1024);
$bytes = Byte::kb(1024); // 1024 kilobytes
$bytes = Byte::mb(50);   // 50 megabytes
$bytes = Byte::gb(2);    // 2 gigabytes

// From human-readable string
$bytes = Byte::fromHumanReadable('2.5 GB');

// From bits
$bytes = Byte::fromBits(8192); // 1024 bytes
```

#### Converting to Different Units

[](#converting-to-different-units)

```
$bytes = new Byte(1536);

// Convert to human-readable string
echo $bytes->toString(); // "1.5 kB"

// Convert to specific units
echo $bytes->toKb();  // "1.5 kB"
echo $bytes->toMb();  // "0.0015 MB"
echo $bytes->toGb();  // "0.000001 GB"

// Customize format
echo $bytes->to('kb', 3, '_'); // "1.500_kB"

// Get raw value in specific unit
$kbValue = $bytes->getValue('kb'); // 1.5
```

#### Comparison Operations

[](#comparison-operations)

The class supports both single-value and multi-value comparisons with two modes:

- `MODE_ALL`: Returns true only if the condition is true for all values
- `MODE_ANY`: Returns true if the condition is true for at least one value

```
$bytes = Byte::kb(1024); // 1 MB

// Compare with a single value
$bytes->equalTo('1 MB');        // true
$bytes->greaterThan('900 kB');  // true
$bytes->lessThan('1.1 MB');     // true

// Compare with multiple values
$bytes->greaterThan(['900 kB', '1.1 MB'], Byte::MODE_ANY); // true
$bytes->greaterThan(['900 kB', '1.1 MB'], Byte::MODE_ALL); // false

// Other comparison methods
$bytes->lessThanOrEqual('1 MB');                    // true
$bytes->greaterThanOrEqual(['1 MB', '1024 kB']);    // true
$bytes->between('900 kB', '1.1 MB');                // true
$bytes->inRanges([['500 kB', '800 kB'], ['1 MB', '1.5 MB']]); // true
```

#### Arithmetic Operations

[](#arithmetic-operations)

```
$bytes = Byte::mb(1);

// Addition
$newBytes = $bytes->increment('500 kB'); // 1.5 MB

// Subtraction
$newBytes = $bytes->decrement('512 kB'); // ~0.5 MB

// Division
$newBytes = $bytes->divide(2); // 512 KB

// Multiplication
$newBytes = $bytes->multiply(3); // 3 MB

// Modulo
$newBytes = $bytes->modulo('512 kB'); // 0

// Absolute value
$newBytes = (new Byte(-1024))->abs(); // 1024 bytes

// Min/Max
$newBytes = $bytes->max('1.5 MB'); // 1.5 MB
$newBytes = $bytes->min(['2 MB', '500 kB']); // 500 kB
```

#### Static Operations on Collections

[](#static-operations-on-collections)

```
// Create a range
$range = Byte::range('1 MB', '5 MB', '1 MB'); // [1 MB, 2 MB, 3 MB, 4 MB, 5 MB]

// Calculate sum
$sum = Byte::sum(['1 MB', '2 MB', '500 kB']); // 3.5 MB

// Calculate average
$avg = Byte::average(['1 MB', '2 MB', '3 MB']); // 2 MB

// Find maximum/minimum
$max = Byte::maximum(['1 MB', '500 kB', '2 GB']); // 2 GB
$min = Byte::minimum(['1 MB', '500 kB', '2 GB']); // 500 kB
```

### BitRate Class

[](#bitrate-class)

#### Creating BitRate Instances

[](#creating-bitrate-instances)

```
use Bermuda\Stdlib\BitRate;

// From numeric value (bits per second)
$rate = new BitRate(1_000_000); // 1 Mbps

// Using static factory methods for bit-based rates
$rate = BitRate::bps(1000);     // 1000 bits per second
$rate = BitRate::kbps(1000);    // 1000 kilobits per second
$rate = BitRate::mbps(10);      // 10 megabits per second
$rate = BitRate::gbps(1);       // 1 gigabit per second

// Using static factory methods for byte-based rates
$rate = BitRate::bytesPerSec(125_000);  // 125 KB/s (equivalent to 1 Mbps)
$rate = BitRate::kBps(1);               // 1 kilobyte per second (8 kbps)
$rate = BitRate::mBps(1);               // 1 megabyte per second (8 Mbps)
$rate = BitRate::gBps(1);               // 1 gigabyte per second (8 Gbps)

// From any unit string
$rate = BitRate::from(10, 'Mbps');      // 10 Mbps
$rate = BitRate::from(1.5, 'GBps');     // 1.5 GB/s

// From human-readable string
$rate = BitRate::fromHumanReadable('10 Mbps');
```

#### Converting Between Units

[](#converting-between-units)

```
$rate = BitRate::mbps(100);  // 100 Mbps

// Get value in bits or bytes
$bitsPerSec = $rate->toBits();    // 100,000,000 bps
$bytesPerSec = $rate->toBytes();  // 12,500,000 B/s

// Convert to human-readable formats
echo $rate->toString();           // "100 Mbps"
echo $rate->toString('byte');     // "12.5 MBps"

// Convert to specific units
echo $rate->toMbps();             // "100 Mbps"
echo $rate->toGbps();             // "0.1 Gbps"
echo $rate->toMBps();             // "12.5 MBps"
echo $rate->toKBps();             // "12500 kBps"

// Customize output format
echo $rate->to('Mbps', 3, '_');   // "100.000_Mbps"
```

#### Comparison Operations

[](#comparison-operations-1)

```
$rate = BitRate::mbps(100);  // 100 Mbps

// Compare with another bit rate
$rate->equalTo(BitRate::kbps(100000));           // true
$rate->equalTo('100 Mbps');                      // true
$rate->equalTo('12.5 MBps');                     // true (bytes equivalent)

// Greater/less than comparisons
$rate->greaterThan(BitRate::mbps(50));           // true
$rate->lessThan(BitRate::gbps(1));               // true

// Compare with arrays
$rate->greaterThan(['10 Mbps', '150 Mbps'], BitRate::MODE_ANY);  // true
```

#### Arithmetic Operations

[](#arithmetic-operations-1)

```
$rate = BitRate::mbps(100);

// Addition
$newRate = $rate->increment(BitRate::mbps(50));    // 150 Mbps

// Subtraction
$newRate = $rate->decrement(BitRate::mbps(30));    // 70 Mbps

// Multiplication
$newRate = $rate->multiply(2);                     // 200 Mbps

// Division
$newRate = $rate->divide(4);                       // 25 Mbps

// Throttling (special case of multiplication)
$throttledRate = $rate->throttle(0.8);             // 80 Mbps (80% of original)
```

#### Transfer Calculations

[](#transfer-calculations)

```
$rate = BitRate::mbps(100);      // 100 Mbps download speed
$fileSize = Byte::gb(1);         // 1 GB file

// Calculate transfer time
$seconds = $rate->calculateTransferTime($fileSize);  // 80 seconds

// Get formatted transfer time
$time = $rate->getFormattedTransferTime($fileSize);  // "1 minute, 20 seconds"

// Calculate transfer amount for a given time
$downloadedSize = $rate->calculateTransferAmount(60);  // 750 MB in 60 seconds

// Estimate file size for streaming
$streamingRate = BitRate::mbps(5);                    // 5 Mbps video stream
$videoDuration = 3600;                                // 1 hour in seconds
$videoSize = $streamingRate->estimateFileSize($videoDuration);  // ~2.25 GB
```

#### Static Operations on Collections

[](#static-operations-on-collections-1)

```
$rates = [
    BitRate::mbps(10),
    BitRate::mbps(50),
    BitRate::mbps(100)
];

// Calculate average
$avgRate = BitRate::average($rates);  // 53.33 Mbps

// Find maximum/minimum
$maxRate = BitRate::maximum($rates);  // 100 Mbps
$minRate = BitRate::minimum($rates);  // 10 Mbps

// Calculate sum
$totalRate = BitRate::sum($rates);    // 160 Mbps

// Create a range
$rangeRates = BitRate::range(
    BitRate::mbps(10),
    BitRate::mbps(50),
    BitRate::mbps(10)
);  // [10 Mbps, 20 Mbps, 30 Mbps, 40 Mbps, 50 Mbps]
```

### BitFormatter for Internationalization

[](#bitformatter-for-internationalization)

The `BitFormatter` class provides formatting functionality with multi-language support:

```
use Bermuda\Stdlib\BitFormatter;
use Bermuda\Stdlib\Byte;
use Bermuda\Stdlib\BitRate;

// Load translation files
BitFormatter::loadLanguage('/path/to/translations/en.php');
BitFormatter::loadLanguage('/path/to/translations/fr.php');
BitFormatter::loadLanguage('/path/to/translations/ru.php');

// Or load all translations from a directory
BitFormatter::loadLanguagesFromDirectory('/path/to/translations');

/* The library comes with built-in translations for multiple languages. You can load all available translations at once using the `loadDefaults()` method*/
BitFormatter::loadDefaults()

// Format time in different languages
$fileSize = Byte::gb(2);
$downloadSpeed = BitRate::mbps(25);
$seconds = BitFormatter::calculateTransferTime($fileSize, $downloadSpeed);

echo BitFormatter::formatTime($seconds, 'en');  // "10 minutes, 40 seconds"
echo BitFormatter::formatTime($seconds, 'fr');  // "10 minutes et 40 secondes"
echo BitFormatter::formatTime($seconds, 'ru');  // "10 минут и 40 секунд"

// Set default language
BitFormatter::setDefaultLanguage('fr');
echo BitFormatter::formatTime($seconds);        // "10 minutes et 40 secondes"

// Direct formatting with BitRate and Byte classes
echo $downloadSpeed->getFormattedTransferTime($fileSize, 'en');  // "10 minutes, 40 seconds"
echo $fileSize->getFormattedTransferTime($downloadSpeed, 'ru');  // "10 минут и 40 секунд"

// Format data values
echo BitFormatter::humanizeBytes(1536);  // "1.5 kB"
echo BitFormatter::humanizeBitRate(1_000_000, 'bit');  // "1 Mbps"
echo BitFormatter::humanizeBitRate(1_000_000, 'byte');  // "125 kBps"
```

Error Handling
--------------

[](#error-handling)

The classes throw exceptions in the following situations:

- `\InvalidArgumentException`: When parsing invalid string formats or using unsupported units
- `\LogicException`: When attempting to decrement by a value greater than the current value
- `\DivisionByZeroError`: When attempting to divide by zero

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance49

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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 ~194 days

Total

5

Last Release

373d ago

Major Versions

v1.2 → v2.02025-05-09

PHP version history (2 changes)v1.0PHP ^8.1

v2.0PHP ^8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/20490712?v=4)[Andrey Shelamkoff](/maintainers/Shelamkoff)[@Shelamkoff](https://github.com/Shelamkoff)

---

Top Contributors

[![Shelamkoff](https://avatars.githubusercontent.com/u/20490712?v=4)](https://github.com/Shelamkoff "Shelamkoff (72 commits)")

---

Tags

bytebytesbytes-conversionphp8unit-conversionutilsvalue-objectconversionformattingunitsbytesfile-sizedata-size

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bermudaphp-byte/health.svg)

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

###  Alternatives

[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k37.7M472](/packages/spatie-laravel-medialibrary)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k9.2M108](/packages/cuyz-valinor)[florianv/swap

Exchange rates library for PHP

1.3k6.4M16](/packages/florianv-swap)[spatie/color

A little library to handle color conversions

38018.9M28](/packages/spatie-color)[php-units-of-measure/php-units-of-measure

A PHP library for converting between standard units of measure.

3123.4M20](/packages/php-units-of-measure-php-units-of-measure)[florianv/exchanger

Currency exchange rates framework for PHP

1874.7M15](/packages/florianv-exchanger)

PHPackages © 2026

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