PHPackages                             coduo/php-humanizer - 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. coduo/php-humanizer

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

coduo/php-humanizer
===================

Humanize values that are readable only for developers

5.0.3(6mo ago)1.7k865.8k↑52.2%11015MITPHPPHP ~8.3 || ~8.4 || ~8.5CI passing

Since Jun 12Pushed 6mo ago28 watchersCompare

[ Source](https://github.com/coduo/php-humanizer)[ Packagist](https://packagist.org/packages/coduo/php-humanizer)[ Fund](https://flow-php.com/sponsor)[ GitHub Sponsors](https://github.com/norberttech)[ RSS](/packages/coduo-php-humanizer/feed)WikiDiscussions 5.x Synced 3d ago

READMEChangelog (10)Dependencies (3)Versions (30)Used By (15)

PHP Humanizer
=============

[](#php-humanizer)

[![Tests](https://github.com/coduo/php-humanizer/actions/workflows/tests.yml/badge.svg?branch=5.x)](https://github.com/coduo/php-humanizer/actions/workflows/tests.yml)[![Latest Stable Version](https://camo.githubusercontent.com/d861b0625e25b50d052fc3faf307a8b0cd4b41ab4bec889f370c962957266d29/68747470733a2f2f706f7365722e707567782e6f72672f636f64756f2f7068702d68756d616e697a65722f762f737461626c65)](https://packagist.org/packages/coduo/php-humanizer)[![Total Downloads](https://camo.githubusercontent.com/8fade21a282286d1034c03dd2c35aac6e1c63e70e91e29cc4044f6135cedd005/68747470733a2f2f706f7365722e707567782e6f72672f636f64756f2f7068702d68756d616e697a65722f646f776e6c6f616473)](https://packagist.org/packages/coduo/php-humanizer)[![Latest Unstable Version](https://camo.githubusercontent.com/8c50c99bb98595b7c5a05cecbe3ea02e14b8ed9a52ee2ea82d25e67cc17c2f6d/68747470733a2f2f706f7365722e707567782e6f72672f636f64756f2f7068702d68756d616e697a65722f762f756e737461626c65)](https://packagist.org/packages/coduo/php-humanizer)[![License](https://camo.githubusercontent.com/7f57ec57133913755774177350aab286ee602803d65da41fb60316b2ff65eabf/68747470733a2f2f706f7365722e707567782e6f72672f636f64756f2f7068702d68756d616e697a65722f6c6963656e7365)](https://packagist.org/packages/coduo/php-humanizer)

[Readme for 5.x version](https://github.com/coduo/php-humanizer/tree/5.x/README.md)

Humanize values to make them readable for regular people ;)

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

[](#installation)

Run the following command:

```
composer require coduo/php-humanizer
```

Usage
=====

[](#usage)

Text
----

[](#text)

**Humanize**

```
use Coduo\PHPHumanizer\StringHumanizer;

StringHumanizer::humanize('field_name'); // "Field Name"
StringHumanizer::humanize('user_id'); // "User id"
StringHumanizer::humanize('field_name', false); // "field name"
```

**Truncate**

Truncate string to word closest to a certain length

```
use Coduo\PHPHumanizer\StringHumanizer;

$text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';

StringHumanizer::truncate($text, 8); // "Lorem ipsum"
StringHumanizer::truncate($text, 8, '...'); // "Lorem ipsum..."
StringHumanizer::truncate($text, 2); // "Lorem"
StringHumanizer::truncate($text, strlen($text)); // "Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc."
```

**Truncate HTML**

Truncate and HTML string to word closest to a certain length

```
use Coduo\PHPHumanizer\StringHumanizer;

$text = 'HyperText Markup Language, commonly referred to as HTML, is the standard markup language used to create web pages.[1] Web browsers can read HTML files and render them into visible or audible web pages. HTML describes the structure of a website semantically along with cues for presentation, making it a markup language, rather than a programming language.';

StringHumanizer::truncateHtml($text, 3); // "HyperText"
StringHumanizer::truncateHtml($text, 12, ''); // "HyperText Markup"
StringHumanizer::truncateHtml($text, 50, '', '...'); // "HyperText Markup Language, commonly referred to as..."
StringHumanizer::truncateHtml($text, 75, '', '...'); // 'HyperText Markup Language, commonly referred to as HTML, is the standard markup...'
```

**Remove shortcodes**

```
$text = 'A text with [short]random[/short] [codes]words[/codes].';
StringHumanizer::removeShortcodes($text); // "A text with ."
StringHumanizer::removeShortcodeTags($text); // "A text with random words."
```

Number
------

[](#number)

**Ordinalize**

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::ordinalize(0); // "0th"
NumberHumanizer::ordinalize(1); // "1st"
NumberHumanizer::ordinalize(2); // "2nd"
NumberHumanizer::ordinalize(23); // "23rd"
NumberHumanizer::ordinalize(1002, 'nl'); // "1002e"
NumberHumanizer::ordinalize(-111); // "-111th"
```

**Ordinal**

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::ordinal(0); // "th"
NumberHumanizer::ordinal(1); // "st"
NumberHumanizer::ordinal(2); // "nd"
NumberHumanizer::ordinal(23); // "rd"
NumberHumanizer::ordinal(1002); // "nd"
NumberHumanizer::ordinal(-111, 'nl'); // "e"
```

**Roman numbers**

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::toRoman(1); // "I"
NumberHumanizer::toRoman(5); // "V"
NumberHumanizer::toRoman(1300); // "MCCC"

NumberHumanizer::fromRoman("MMMCMXCIX"); // 3999
NumberHumanizer::fromRoman("V"); // 5
NumberHumanizer::fromRoman("CXXV"); // 125
```

**Binary Suffix**

Convert a number of bytes in to the highest applicable data unit

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::binarySuffix(0); // "0 bytes"
NumberHumanizer::binarySuffix(1); // "1 bytes"
NumberHumanizer::binarySuffix(1024); // "1 kB"
NumberHumanizer::binarySuffix(1025); // "1 kB"
NumberHumanizer::binarySuffix(1536); // "1.5 kB"
NumberHumanizer::binarySuffix(1048576 * 5); // "5 MB"
NumberHumanizer::binarySuffix(1073741824 * 2); // "2 GB"
NumberHumanizer::binarySuffix(1099511627776 * 3); // "3 TB"
NumberHumanizer::binarySuffix(1325899906842624); // "1.18 PB"
```

Number can be also formatted for specific locale

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::binarySuffix(1536, 'pl'); // "1,5 kB"
```

Number can also be humanized with a specific number of decimal places with `preciseBinarySuffix($number, $precision, $locale = 'en')`The precision parameter must be between 0 and 3.

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::preciseBinarySuffix(1024, 2); // "1.00 kB"
NumberHumanizer::preciseBinarySuffix(1325899906842624, 3); // "1.178 PB"
```

This function also supports locale

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::preciseBinarySuffix(1325899906842624, 3, 'pl'); // "1,178 PB"
```

**Metric Suffix**

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::metricSuffix(-1); // "-1"
NumberHumanizer::metricSuffix(0); // "0"
NumberHumanizer::metricSuffix(1); // "1"
NumberHumanizer::metricSuffix(101); // "101"
NumberHumanizer::metricSuffix(1000); // "1k"
NumberHumanizer::metricSuffix(1240); // "1.2k"
NumberHumanizer::metricSuffix(1240000); // "1.24M"
NumberHumanizer::metricSuffix(3500000); // "3.5M"
```

Number can be also formatted for specific locale

```
use Coduo\PHPHumanizer\NumberHumanizer;

NumberHumanizer::metricSuffix(1240000, 'pl'); // "1,24M"
```

Collections
-----------

[](#collections)

**Oxford**

```
use Coduo\PHPHumanizer\CollectionHumanizer;

CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others"
CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other"
CollectionHumanizer::oxford(['Michal', 'Norbert']); // "Michal and Norbert"
```

Oxford is using translator component, so you can use whatever string format you like.

Date time
---------

[](#date-time)

**Difference**

```
use Coduo\PHPHumanizer\DateTimeHumanizer;

DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:00")); // just now
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:05")); // 5 seconds from now
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:59:00")); // 1 minute ago
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:45:00")); // 15 minutes ago
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:15:00")); // 15 minutes from now
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 14:00:00")); // 1 hour from now
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 15:00:00")); // 2 hours from now
DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:00:00")); // 1 hour ago
DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-25")); // 1 day ago
DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-24")); // 2 days ago
DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-28")); // 2 days from now
DateTimeHumanizer::difference(new \DateTime("2014-04-01"), new \DateTime("2014-04-15")); // 2 weeks from now
DateTimeHumanizer::difference(new \DateTime("2014-04-15"), new \DateTime("2014-04-07")); // 1 week ago
DateTimeHumanizer::difference(new \DateTime("2014-01-01"), new \DateTime("2014-04-01")); // 3 months from now
DateTimeHumanizer::difference(new \DateTime("2014-05-01"), new \DateTime("2014-04-01")); // 1 month ago
DateTimeHumanizer::difference(new \DateTime("2015-05-01"), new \DateTime("2014-04-01")); // 1 year ago
DateTimeHumanizer::difference(new \DateTime("2014-05-01"), new \DateTime("2016-04-01")); // 2 years from now
```

**Precise difference**

```
use Coduo\PHPHumanizer\DateTimeHumanizer;

DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-25 11:20:00")); // 1 day, 1 hour, 40 minutes ago
DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2015-04-28 17:00:00")); // 1 year, 2 days, 4 hours from now
DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2016-04-27 13:00:00")); // 2 years, 1 day from now
```

Aeon Calendar
-------------

[](#aeon-calendar)

[Aeon PHP](https://aeon-php.org/) is a date&amp;time oriented set of libraries.

```
use Coduo\PHPHumanizer\DateTimeHumanizer;

$timeUnit = TimeUnit::days(2)
                ->add(TimeUnit::hours(3))
                ->add(TimeUnit::minutes(25))
                ->add(TimeUnit::seconds(30))
                ->add(TimeUnit::milliseconds(200));

DateTimeHumanizer::timeUnit($timeUnit); // 2 days, 3 hours, 25 minutes, and 30.2 seconds
```

Currently we support following languages:

- [Azerbaijani](src/Coduo/PHPHumanizer/Resources/translations/difference.az.yml)
- [English](src/Coduo/PHPHumanizer/Resources/translations/difference.en.yml)
- [Polish](src/Coduo/PHPHumanizer/Resources/translations/difference.pl.yml)
- [German](src/Coduo/PHPHumanizer/Resources/translations/difference.de.yml)
- [Turkish](src/Coduo/PHPHumanizer/Resources/translations/difference.tr.yml)
- [French](src/Coduo/PHPHumanizer/Resources/translations/difference.fr.yml)
- [Português - Brasil](src/Coduo/PHPHumanizer/Resources/translations/difference.pt_BR.yml)
- [Italian](src/Coduo/PHPHumanizer/Resources/translations/difference.it.yml)
- [Dutch](src/Coduo/PHPHumanizer/Resources/translations/difference.nl.yml)
- [Русский](src/Coduo/PHPHumanizer/Resources/translations/difference.ru.yml)
- [Norwegian](src/Coduo/PHPHumanizer/Resources/translations/difference.no.yml)
- [Afrikaans](src/Coduo/PHPHumanizer/Resources/translations/difference.af.yml)
- [Bulgarian](src/Coduo/PHPHumanizer/Resources/translations/difference.bg.yml)
- [Indonesian](src/Coduo/PHPHumanizer/Resources/translations/difference.id.yml)
- [Chinese Simplified](src/Coduo/PHPHumanizer/Resources/translations/difference.zh_CN.yml)
- [Chinese Taiwan](src/Coduo/PHPHumanizer/Resources/translations/difference.zh_TW.yml)
- [Spanish](src/Coduo/PHPHumanizer/Resources/translations/difference.es.yml)
- [Ukrainian](src/Coduo/PHPHumanizer/Resources/translations/difference.uk.yml)
- [Danish](src/Coduo/PHPHumanizer/Resources/translations/difference.da.yml)
- [Thai](src/Coduo/PHPHumanizer/Resources/translations/difference.th.yml)
- [Japanese](src/Coduo/PHPHumanizer/Resources/translations/difference.ja.yml)
- [Romanian](src/Coduo/PHPHumanizer/Resources/translations/difference.ro.yml)

Development
===========

[](#development)

After downloading library update dependencies:

```
composer update

```

In order to check lowest possible versions of dependencies add

```
composer update --prefer-lowest

```

Execute test suite:

```
composer run test

```

Run CS Fixer

```
composer run cs:php:fix

```

Credits
=======

[](#credits)

This lib was inspired by [Java Humanize Lib](https://github.com/mfornos/humanize) &amp;&amp; [Rails Active Support](https://github.com/rails/rails/tree/master/activesupport/lib/active_support)

###  Health Score

70

—

ExcellentBetter than 100% of packages

Maintenance66

Regular maintenance activity

Popularity64

Solid adoption and visibility

Community45

Growing community involvement

Maturity91

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

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

Total

29

Last Release

204d ago

Major Versions

1.0.9 → 2.0.02016-02-21

2.0.1 → 3.0.02019-04-20

1.0.x-dev → 4.0.02021-02-24

4.x-dev → 5.0.02024-04-11

PHP version history (7 changes)1.0.0PHP &gt;=5.3.0

3.0.0PHP &gt;=7.0.0

4.0.0PHP ^7.4 | ^8.0

4.0.3PHP ~7.4 || ~8.0 || ~8.1

5.0.0PHP ~8.1 || ~8.2 || ~8.3

5.0.1PHP ~8.2 || ~8.3 || ~8.4

5.0.3PHP ~8.3 || ~8.4 || ~8.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/52325810?v=4)[norbert-tech](/maintainers/norbert-tech)[@norbert-tech](https://github.com/norbert-tech)

![](https://www.gravatar.com/avatar/6b69a1ca71c5d2ed554cb1da909c737cb4a88151bf67ca894b80379976085689?d=identicon)[defrag](/maintainers/defrag)

---

Top Contributors

[![norberttech](https://avatars.githubusercontent.com/u/1921950?v=4)](https://github.com/norberttech "norberttech (94 commits)")[![aeon-automation](https://avatars.githubusercontent.com/u/77585774?v=4)](https://github.com/aeon-automation "aeon-automation (30 commits)")[![defrag](https://avatars.githubusercontent.com/u/15900?v=4)](https://github.com/defrag "defrag (14 commits)")[![mostertb](https://avatars.githubusercontent.com/u/2180195?v=4)](https://github.com/mostertb "mostertb (10 commits)")[![doenietzomoeilijk](https://avatars.githubusercontent.com/u/648663?v=4)](https://github.com/doenietzomoeilijk "doenietzomoeilijk (8 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (7 commits)")[![sam002](https://avatars.githubusercontent.com/u/2215228?v=4)](https://github.com/sam002 "sam002 (6 commits)")[![Borales](https://avatars.githubusercontent.com/u/1118933?v=4)](https://github.com/Borales "Borales (6 commits)")[![sarelvdwalt](https://avatars.githubusercontent.com/u/2071493?v=4)](https://github.com/sarelvdwalt "sarelvdwalt (5 commits)")[![hyperpanic](https://avatars.githubusercontent.com/u/14869931?v=4)](https://github.com/hyperpanic "hyperpanic (4 commits)")[![thunderer](https://avatars.githubusercontent.com/u/1087180?v=4)](https://github.com/thunderer "thunderer (4 commits)")[![4t87ux8](https://avatars.githubusercontent.com/u/5392145?v=4)](https://github.com/4t87ux8 "4t87ux8 (3 commits)")[![arrowrowe](https://avatars.githubusercontent.com/u/4508882?v=4)](https://github.com/arrowrowe "arrowrowe (3 commits)")[![lpopov](https://avatars.githubusercontent.com/u/837343?v=4)](https://github.com/lpopov "lpopov (3 commits)")[![naprirfan](https://avatars.githubusercontent.com/u/1271830?v=4)](https://github.com/naprirfan "naprirfan (3 commits)")[![omissis](https://avatars.githubusercontent.com/u/197604?v=4)](https://github.com/omissis "omissis (3 commits)")[![ozmodiar](https://avatars.githubusercontent.com/u/512738?v=4)](https://github.com/ozmodiar "ozmodiar (3 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")[![dagaa](https://avatars.githubusercontent.com/u/782188?v=4)](https://github.com/dagaa "dagaa (2 commits)")[![brianwozeniak](https://avatars.githubusercontent.com/u/1310816?v=4)](https://github.com/brianwozeniak "brianwozeniak (2 commits)")

---

Tags

phphumanizehumanizer

### Embed Badge

![Health badge](/badges/coduo-php-humanizer/health.svg)

```
[![Health](https://phpackages.com/badges/coduo-php-humanizer/health.svg)](https://phpackages.com/packages/coduo-php-humanizer)
```

###  Alternatives

[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M388](/packages/easycorp-easyadmin-bundle)[flarum/core

Delightfully simple forum software.

201.4M2.3k](/packages/flarum-core)[metamodels/core

MetaModels core

10156.4k68](/packages/metamodels-core)[symfony/ux-toggle-password

Toggle visibility of password inputs for Symfony Forms

27600.4k5](/packages/symfony-ux-toggle-password)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1616.4k14](/packages/2lenet-crudit-bundle)

PHPackages © 2026

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