rhdevelopment/numbers-for-humans

Format numbers, sizes, and dates into human readable strings.

Maintainers

Package info

github.com/hadley8899/NumbersForHumans

pkg:composer/rhdevelopment/numbers-for-humans

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2025-10-23 19:57 UTC

This package is auto-updated.

Last update: 2026-03-24 11:23:42 UTC


README

Turn plain numbers, dates, and byte counts into friendly, human-oriented strings.
rhdevelopment/numbers-for-humans wraps a handful of common formatting tasks into a single, lightweight helper class.

Installation

composer require rhdevelopment/numbers-for-humans

The package requires PHP ^7.4 || ^8.0, the intl extension, and nesbot/carbon.

Quick start

<?php

require __DIR__ . '/vendor/autoload.php';

use RHDevelopment\Readable\Readable;

echo Readable::getHumanNumber(1524999); // 1.5M
echo Readable::getDateTime('2020-08-26 17:38:23'); // Wednesday, August 26, 2020 05:38 PM
echo Readable::getSize(1500); // 1.5 KB

All methods are static, so you can call them directly from anywhere in your project.

API overview

Helper Description
getNumber(int $input, string $delimiter = ','): string Format integers with thousands separators.
`getHumanNumber(int float
`readableString(int float $input, string $lang = 'en'): string`
`getDecimal(int float $input, int $decimals = 2, string $point = '.', string $delimiter = ','): string`
`getDecInt(int float $input, int $decimals = 2, string $point = '.', string $delimiter = ','): string`
getDate(string $input, string $timezone = null): string Produce day month year strings from ISO-ish inputs or Carbon instances.
`getTime(string Carbon\Carbon $input, bool $hasSeconds = false, string $timezone = null): string`
`getDateTime(string Carbon\Carbon $input, bool $hasSeconds = false, string $timezone = null): string`
`getDiffDateTime(string Carbon\Carbon $old, string
getTimeLength(int $seconds, string $join = ' ', bool $short = false): string Convert durations in seconds to human friendly intervals.
`getDateTimeLength(string Carbon\Carbon $old, string
getSize(int $bytes, bool $decimal = true): ?string Convert byte counts into KB/MB/TB (decimal) or KiB/MiB/TiB (binary) units.

Detailed examples

Readable::getNumber(1234567890);                // "1,234,567,890"
Readable::getNumber(-1234567, ' ');             // "-1 234 567"

Readable::getHumanNumber(1524999);              // "1.5M"
Readable::getHumanNumber(1524999, true, 2);     // "1.52M"
Readable::getHumanNumber(-3400000, false);      // "-3M"

Readable::readableString(12345);                // "twelve thousand three hundred forty-five"
Readable::readableString(-42);                  // "minus forty-two"

Readable::getDecimal(1234.567, 2);              // "1,234.57"
Readable::getDecInt(200.0);                     // "200"

Readable::getDate('2020-08-26 17:38:23');       // "26 August 2020"
Readable::getTime('2020-08-26 17:38:23', true); // "17:38:23"
Readable::getDateTime('2020-08-26 17:38:23');   // "Wednesday, August 26, 2020 05:38 PM"

$old = '2020-01-22 05:58:00';
$new = '2020-01-23 05:58:00';
Readable::getDiffDateTime($old, $new);          // "1 day before"
Readable::getTimeLength(3661, ', ', true);      // "1h, 1m, 1s"
Readable::getDateTimeLength($new, $old);        // "1 day after"

Readable::getSize(1500);                        // "1.5 KB"
Readable::getSize(1024 ** 2, false);            // "1 MiB"

CLI demo

Install the package (or run composer install inside this repository) and execute:

vendor/bin/numbers-for-humans

You’ll see every helper in action with sample inputs and outputs. The CLI script automatically locates the Composer autoloader whether you’re working from this repository or using the package as a dependency.

Web demo

There’s also a small showcase page at examples/index.php.

composer install
php -S 127.0.0.1:8000 -t examples

Then browse to http://127.0.0.1:8000 to experiment interactively.

Testing

Run the full PHPUnit suite:

composer install
composer test

The test suite covers every helper, including edge cases for negative numbers, timezone conversions, and binary versus decimal file size calculations.

Releasing on Packagist

  1. Ensure the repository is public on GitHub.
  2. Tag a semantic version: git tag v1.0.0 && git push --tags.
  3. Submit the repository URL (https://github.com/rhdevelopment/NumbersForHumans) to Packagist.org.
  4. Configure the GitHub service hook or Packagist auto-update to keep releases in sync.

Once published, others can install the package with composer require rhdevelopment/numbers-for-humans.

License

Licensed under the MIT License.