{"id":13792486,"url":"https://github.com/coduo/php-humanizer","last_synced_at":"2025-05-13T15:13:14.984Z","repository":{"id":16277873,"uuid":"19026264","full_name":"coduo/php-humanizer","owner":"coduo","description":"Humanize values that are readable only for developers.","archived":false,"fork":false,"pushed_at":"2025-05-05T09:35:49.000Z","size":1165,"stargazers_count":1610,"open_issues_count":0,"forks_count":112,"subscribers_count":28,"default_branch":"5.x","last_synced_at":"2025-05-05T10:38:45.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coduo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-04-22T10:51:03.000Z","updated_at":"2025-05-05T09:35:51.000Z","dependencies_parsed_at":"2024-04-11T11:27:50.609Z","dependency_job_id":"ab3d189b-122c-4df1-84c6-06adfd58a2cf","html_url":"https://github.com/coduo/php-humanizer","commit_stats":{"total_commits":222,"total_committers":51,"mean_commits":4.352941176470588,"dds":0.6621621621621622,"last_synced_commit":"9e7e2c48f8579dc555ec48f28771e82f76089cc4"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coduo%2Fphp-humanizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coduo%2Fphp-humanizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coduo%2Fphp-humanizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coduo%2Fphp-humanizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coduo","download_url":"https://codeload.github.com/coduo/php-humanizer/tar.gz/refs/heads/5.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253969266,"owners_count":21992264,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T22:01:12.733Z","updated_at":"2025-05-13T15:13:09.974Z","avatar_url":"https://github.com/coduo.png","language":"PHP","readme":"# PHP Humanizer\n\n[![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)\n[![Latest Stable Version](https://poser.pugx.org/coduo/php-humanizer/v/stable)](https://packagist.org/packages/coduo/php-humanizer)\n[![Total Downloads](https://poser.pugx.org/coduo/php-humanizer/downloads)](https://packagist.org/packages/coduo/php-humanizer)\n[![Latest Unstable Version](https://poser.pugx.org/coduo/php-humanizer/v/unstable)](https://packagist.org/packages/coduo/php-humanizer)\n[![License](https://poser.pugx.org/coduo/php-humanizer/license)](https://packagist.org/packages/coduo/php-humanizer)\n\n[Readme for 5.x version](https://github.com/coduo/php-humanizer/tree/5.x/README.md)\n\n\n\nHumanize values to make them readable for regular people ;)\n\n# Installation\n\nRun the following command:\n\n```shell\ncomposer require coduo/php-humanizer\n```\n\n# Usage\n\n## Text\n\n**Humanize**\n\n```php\nuse Coduo\\PHPHumanizer\\StringHumanizer;\n\nStringHumanizer::humanize('field_name'); // \"Field Name\"\nStringHumanizer::humanize('user_id'); // \"User id\"\nStringHumanizer::humanize('field_name', false); // \"field name\"\n```\n\n**Truncate**\n\nTruncate string to word closest to a certain length\n\n```php\nuse Coduo\\PHPHumanizer\\StringHumanizer;\n\n$text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';\n\nStringHumanizer::truncate($text, 8); // \"Lorem ipsum\"\nStringHumanizer::truncate($text, 8, '...'); // \"Lorem ipsum...\"\nStringHumanizer::truncate($text, 2); // \"Lorem\"\nStringHumanizer::truncate($text, strlen($text)); // \"Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.\"\n\n```\n\n**Truncate HTML**\n\nTruncate and HTML string to word closest to a certain length\n\n```php\nuse Coduo\\PHPHumanizer\\StringHumanizer;\n\n$text = '\u003cp\u003e\u003cb\u003eHyperText Markup Language\u003c/b\u003e, commonly referred to as \u003cb\u003eHTML\u003c/b\u003e, is the standard \u003ca href=\"/wiki/Markup_language\" title=\"Markup language\"\u003emarkup language\u003c/a\u003e used to create \u003ca href=\"/wiki/Web_page\" title=\"Web page\"\u003eweb pages\u003c/a\u003e.\u003csup id=\"cite_ref-1\" class=\"reference\"\u003e\u003ca href=\"#cite_note-1\"\u003e\u003cspan\u003e[\u003c/span\u003e1\u003cspan\u003e]\u003c/span\u003e\u003c/a\u003e\u003c/sup\u003e \u003ca href=\"/wiki/Web_browser\" title=\"Web browser\"\u003eWeb browsers\u003c/a\u003e can read HTML files and render them into visible or audible web pages. HTML describes the structure of a \u003ca href=\"/wiki/Website\" title=\"Website\"\u003ewebsite\u003c/a\u003e \u003ca href=\"/wiki/Semantic\" title=\"Semantic\" class=\"mw-redirect\"\u003esemantically\u003c/a\u003e along with cues for presentation, making it a markup language, rather than a \u003ca href=\"/wiki/Programming_language\" title=\"Programming language\"\u003eprogramming language\u003c/a\u003e.\u003c/p\u003e';\n\nStringHumanizer::truncateHtml($text, 3); // \"\u003cb\u003eHyperText\u003c/b\u003e\"\nStringHumanizer::truncateHtml($text, 12, ''); // \"HyperText Markup\"\nStringHumanizer::truncateHtml($text, 50, '', '...'); // \"HyperText Markup Language, commonly referred to as...\"\nStringHumanizer::truncateHtml($text, 75, '\u003cb\u003e\u003ci\u003e\u003cu\u003e\u003cem\u003e\u003cstrong\u003e\u003ca\u003e\u003cspan\u003e', '...'); // '\u003cb\u003eHyperText Markup Language\u003c/b\u003e, commonly referred to as \u003cb\u003eHTML\u003c/b\u003e, is the standard \u003ca href=\"/wiki/Markup_language\" title=\"Markup language\"\u003emarkup...\u003c/a\u003e'\n\n```\n\n**Remove shortcodes**\n\n```php\n$text = 'A text with [short]random[/short] [codes]words[/codes].';\nStringHumanizer::removeShortcodes($text); // \"A text with .\"\nStringHumanizer::removeShortcodeTags($text); // \"A text with random words.\"\n```\n\n## Number\n\n**Ordinalize**\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::ordinalize(0); // \"0th\"\nNumberHumanizer::ordinalize(1); // \"1st\"\nNumberHumanizer::ordinalize(2); // \"2nd\"\nNumberHumanizer::ordinalize(23); // \"23rd\"\nNumberHumanizer::ordinalize(1002, 'nl'); // \"1002e\"\nNumberHumanizer::ordinalize(-111); // \"-111th\"\n\n```\n\n**Ordinal**\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::ordinal(0); // \"th\"\nNumberHumanizer::ordinal(1); // \"st\"\nNumberHumanizer::ordinal(2); // \"nd\"\nNumberHumanizer::ordinal(23); // \"rd\"\nNumberHumanizer::ordinal(1002); // \"nd\"\nNumberHumanizer::ordinal(-111, 'nl'); // \"e\"\n```\n\n**Roman numbers**\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::toRoman(1); // \"I\"\nNumberHumanizer::toRoman(5); // \"V\"\nNumberHumanizer::toRoman(1300); // \"MCCC\"\n\nNumberHumanizer::fromRoman(\"MMMCMXCIX\"); // 3999\nNumberHumanizer::fromRoman(\"V\"); // 5\nNumberHumanizer::fromRoman(\"CXXV\"); // 125\n```\n\n**Binary Suffix**\n\nConvert a number of bytes in to the highest applicable data unit\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::binarySuffix(0); // \"0 bytes\"\nNumberHumanizer::binarySuffix(1); // \"1 bytes\"\nNumberHumanizer::binarySuffix(1024); // \"1 kB\"\nNumberHumanizer::binarySuffix(1025); // \"1 kB\"\nNumberHumanizer::binarySuffix(1536); // \"1.5 kB\"\nNumberHumanizer::binarySuffix(1048576 * 5); // \"5 MB\"\nNumberHumanizer::binarySuffix(1073741824 * 2); // \"2 GB\"\nNumberHumanizer::binarySuffix(1099511627776 * 3); // \"3 TB\"\nNumberHumanizer::binarySuffix(1325899906842624); // \"1.18 PB\"\n```\n\nNumber can be also formatted for specific locale\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::binarySuffix(1536, 'pl'); // \"1,5 kB\"\n```\n\nNumber can also be humanized with a specific number of decimal places with `preciseBinarySuffix($number, $precision, $locale = 'en')`\nThe precision parameter must be between 0 and 3.\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::preciseBinarySuffix(1024, 2); // \"1.00 kB\"\nNumberHumanizer::preciseBinarySuffix(1325899906842624, 3); // \"1.178 PB\"\n```\n\nThis function also supports locale\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::preciseBinarySuffix(1325899906842624, 3, 'pl'); // \"1,178 PB\"\n```\n\n**Metric Suffix**\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::metricSuffix(-1); // \"-1\"\nNumberHumanizer::metricSuffix(0); // \"0\"\nNumberHumanizer::metricSuffix(1); // \"1\"\nNumberHumanizer::metricSuffix(101); // \"101\"\nNumberHumanizer::metricSuffix(1000); // \"1k\"\nNumberHumanizer::metricSuffix(1240); // \"1.2k\"\nNumberHumanizer::metricSuffix(1240000); // \"1.24M\"\nNumberHumanizer::metricSuffix(3500000); // \"3.5M\"\n```\n\nNumber can be also formatted for specific locale\n\n```php\nuse Coduo\\PHPHumanizer\\NumberHumanizer;\n\nNumberHumanizer::metricSuffix(1240000, 'pl'); // \"1,24M\"\n```\n\n## Collections\n\n**Oxford**\n\n```php\nuse Coduo\\PHPHumanizer\\CollectionHumanizer;\n\nCollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // \"Michal, Norbert, and 2 others\"\nCollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // \"Michal, Norbert, and 1 other\"\nCollectionHumanizer::oxford(['Michal', 'Norbert']); // \"Michal and Norbert\"\n```\n\nOxford is using translator component, so you can use whatever string format you like.\n\n## Date time\n\n**Difference**\n\n```php\nuse Coduo\\PHPHumanizer\\DateTimeHumanizer;\n\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 13:00:00\")); // just now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 13:00:05\")); // 5 seconds from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 12:59:00\")); // 1 minute ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 12:45:00\")); // 15 minutes ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 13:15:00\")); // 15 minutes from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 14:00:00\")); // 1 hour from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 15:00:00\")); // 2 hours from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-26 12:00:00\")); // 1 hour ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26\"), new \\DateTime(\"2014-04-25\")); // 1 day ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26\"), new \\DateTime(\"2014-04-24\")); // 2 days ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-26\"), new \\DateTime(\"2014-04-28\")); // 2 days from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-01\"), new \\DateTime(\"2014-04-15\")); // 2 weeks from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-04-15\"), new \\DateTime(\"2014-04-07\")); // 1 week ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-01-01\"), new \\DateTime(\"2014-04-01\")); // 3 months from now\nDateTimeHumanizer::difference(new \\DateTime(\"2014-05-01\"), new \\DateTime(\"2014-04-01\")); // 1 month ago\nDateTimeHumanizer::difference(new \\DateTime(\"2015-05-01\"), new \\DateTime(\"2014-04-01\")); // 1 year ago\nDateTimeHumanizer::difference(new \\DateTime(\"2014-05-01\"), new \\DateTime(\"2016-04-01\")); // 2 years from now\n```\n\n**Precise difference**\n\n```php\nuse Coduo\\PHPHumanizer\\DateTimeHumanizer;\n\nDateTimeHumanizer::preciseDifference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2014-04-25 11:20:00\")); // 1 day, 1 hour, 40 minutes ago\nDateTimeHumanizer::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\nDateTimeHumanizer::preciseDifference(new \\DateTime(\"2014-04-26 13:00:00\"), new \\DateTime(\"2016-04-27 13:00:00\")); // 2 years, 1 day from now\n```\n\n## Aeon Calendar \n\n[Aeon PHP](https://aeon-php.org/) is a date\u0026time oriented set of libraries. \n\n```php\nuse Coduo\\PHPHumanizer\\DateTimeHumanizer;\n\n$timeUnit = TimeUnit::days(2)\n                -\u003eadd(TimeUnit::hours(3))\n                -\u003eadd(TimeUnit::minutes(25))\n                -\u003eadd(TimeUnit::seconds(30))\n                -\u003eadd(TimeUnit::milliseconds(200));\n            \nDateTimeHumanizer::timeUnit($timeUnit); // 2 days, 3 hours, 25 minutes, and 30.2 seconds\n```\n\nCurrently we support following languages:\n* [Azerbaijani](src/Coduo/PHPHumanizer/Resources/translations/difference.az.yml)\n* [English](src/Coduo/PHPHumanizer/Resources/translations/difference.en.yml)\n* [Polish](src/Coduo/PHPHumanizer/Resources/translations/difference.pl.yml)\n* [German](src/Coduo/PHPHumanizer/Resources/translations/difference.de.yml)\n* [Turkish](src/Coduo/PHPHumanizer/Resources/translations/difference.tr.yml)\n* [French](src/Coduo/PHPHumanizer/Resources/translations/difference.fr.yml)\n* [Português - Brasil](src/Coduo/PHPHumanizer/Resources/translations/difference.pt_BR.yml)\n* [Italian](src/Coduo/PHPHumanizer/Resources/translations/difference.it.yml)\n* [Dutch](src/Coduo/PHPHumanizer/Resources/translations/difference.nl.yml)\n* [Русский](src/Coduo/PHPHumanizer/Resources/translations/difference.ru.yml)\n* [Norwegian](src/Coduo/PHPHumanizer/Resources/translations/difference.no.yml)\n* [Afrikaans](src/Coduo/PHPHumanizer/Resources/translations/difference.af.yml)\n* [Bulgarian](src/Coduo/PHPHumanizer/Resources/translations/difference.bg.yml)\n* [Indonesian](src/Coduo/PHPHumanizer/Resources/translations/difference.id.yml)\n* [Chinese Simplified](src/Coduo/PHPHumanizer/Resources/translations/difference.zh_CN.yml)\n* [Chinese Taiwan](src/Coduo/PHPHumanizer/Resources/translations/difference.zh_TW.yml)\n* [Spanish](src/Coduo/PHPHumanizer/Resources/translations/difference.es.yml)\n* [Ukrainian](src/Coduo/PHPHumanizer/Resources/translations/difference.uk.yml)\n* [Danish](src/Coduo/PHPHumanizer/Resources/translations/difference.da.yml)\n* [Thai](src/Coduo/PHPHumanizer/Resources/translations/difference.th.yml)\n* [Japanese](src/Coduo/PHPHumanizer/Resources/translations/difference.ja.yml)\n* [Romanian](src/Coduo/PHPHumanizer/Resources/translations/difference.ro.yml)\n\n# Development\n\nAfter downloading library update dependencies:\n\n```\ncomposer update\n```\n\nIn order to check lowest possible versions of dependencies add \n\n```\ncomposer update --prefer-lowest\n````\n\nExecute test suite:\n\n```\ncomposer run test\n```\n\nRun CS Fixer\n\n```\ncomposer run cs:php:fix\n```\n\n\n# Credits\n\nThis lib was inspired by [Java Humanize Lib](https://github.com/mfornos/humanize) \u0026\u0026 [Rails Active Support](https://github.com/rails/rails/tree/master/activesupport/lib/active_support)\n","funding_links":[],"categories":["PHP","类库"],"sub_categories":["未归类"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoduo%2Fphp-humanizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoduo%2Fphp-humanizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoduo%2Fphp-humanizer/lists"}