PHPackages                             cocur/human-date - 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. cocur/human-date

ActiveLibrary

cocur/human-date
================

Transforms dates into a human-readable form

v0.1(11y ago)2823.3k↓100%1[3 issues](https://github.com/cocur/human-date/issues)MITPHPPHP &gt;=5.4

Since May 14Pushed 3y ago2 watchersCompare

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

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

cocur/human-date
================

[](#cocurhuman-date)

> Transforms dates into a human-readable format.

[![Latest Stable Version](https://camo.githubusercontent.com/7b2ba836ea82e06f3cba3d870a1daa58194568caa402925d89f5cad7a8580074/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6375722f68756d616e2d646174652e737667)](https://packagist.org/packages/cocur/human-date)[![Build Status](https://camo.githubusercontent.com/0092234984f6d95f5a251b59b37654847a97bf65a3a242c445593fb33efdf6a0/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f636f6375722f68756d616e2d646174652e737667)](https://travis-ci.org/cocur/human-date)[![Code Coverage](https://camo.githubusercontent.com/9b5ac242028aa243fb6a98f0f73dee9c441ad0e26247889d8ecaf9f517dc892c/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f636f6375722f68756d616e2d646174652e737667)](https://coveralls.io/r/cocur/human-date)

Features
--------

[](#features)

- Transforms dates into a human-readable format
- Supports translatable strings
- No external dependencies.
- PSR-4 compatible.
- Compatible with PHP &gt;= 5.4 and [HHVM](http://hhvm.com).
- Integrations for [Symfony2](http://symfony.com) and [Twig](http://twig.sensiolabs.org).

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

[](#installation)

You can install `cocur/human-date` using [Composer](https://getcomposer.org):

```
$ composer require cocur/human-date:@stable
```

*In a production environment you should replace `@stable` with the [version](https://github.com/cocur/human-date/releases) you want to use.*

Usage
-----

[](#usage)

You can pass an instance of `DateTime` to the `HumanDate::transform()` method. For example, assuming that today is `2012-08-18`:

```
use Cocur\HumanDate\HumanDate;

$humanDate = new HumanDate();

echo $humanDate->transform(new DateTime('now'));
// 'Today'

echo $humanDate->transform(new DateTime('+1 day'));
// 'Tomorrow'

echo $humanDate->transform(new DateTime('-1 day'));
// 'Yesterday'

echo $humanDate->transform(new DateTime('2012-08-21'));
// 'Next Tuesday'

echo $humanDate->transform(new DateTime('2012-09-30'));
// 'September 30'

echo $humanDate->transform(new DateTime('2013-03-30'));
// 'March 30, 2013'
```

Translation
-----------

[](#translation)

HumanDate supports translation of strings. The `Cocur\HumanDate\HumanDate` constructor accepts an instance of `Cocur\HumanDate\Translation\TranslationInterface`.

```
$translation = MyTranslation(); // must implement `Cocur\HumanDate\Translation\TranslationInterface`
$humanDate = new HumanDate($translation);

echo $humanDate->transform(new DateTime('now'));
// Calls MyTranslation::trans()
```

Additionally the library includes an adapter for the [Symfony Translation](http://symfony.com/doc/current/components/translation/index.html) component.

Bridges
-------

[](#bridges)

`cocur/human-date` contains bridges for Symfony and Twig.

### Symfony

[](#symfony)

The Symfony bridge provides you with a bundle and an extension to use `HumanDate` as a service in your application.

```
# app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Cocur\HumanDate\Bridge\Symfony\CocurHumanDateBundle(),
        );
        // ...
    }

    // ...
}
```

You can now use the `cocur_human_date` service everywhere in your application, for example, in your controller:

```
$slug = $this->get('cocur_human_date')->slugify(new DateTime('2014-04-14'));
```

The bundle also provides an alias `human_date` for the `cocur_human_date` service:

```
$slug = $this->get('human_date')->slugify(new DateTime('2014-04-14'));
```

#### Translation

[](#translation-1)

HumanDate includes an adapter for the [Symfony Translation](http://symfony.com/doc/current/components/translation/index.html) component. The adapter requires an instance of `Symfony\Component\Translation\TranslatorInterface` and additionally accepts a translation domain and locale. The adapters `trans()` method passes theses values to every call of `Symfony\Component\Translation\TranslatorInterface::trans()`.

```
use Cocur\HumanDate\Bridge\Symfony\Translation\SymfonyTranslation;
use Cocur\HumanDate\HumanDate;

// Get or create an instance of Symfony\Component\Translation\TranslatorInterface
// For example, inside a controller
$sfTrans = $this->get('translation');

// Create an adapter with translation domain "human_date" and locale "en"
// trans() passes domain and locale to every call of Symfony\Component\Translation\TranslatorInterface::trans()
// If you omit the domain and locale it uses the defaults.
$trans = new SymfonyTranslation($sfTrans, 'human_date', 'en');

$humanDate = new HumanDate($trans);
```

### Twig

[](#twig)

If you use the Symfony2 framework with Twig you can use the Twig filter `humanDate` in your templates after you have setup Symfony2 integrations (see above).

```
{{ post.createdAt|humanDate }}
```

If you use Twig outside of the Symfony2 framework you first need to add the extension to your environment:

```
use Cocur\HumanDate\Bridge\Twig\HumanDateExtension;
use Cocur\HumanDate\HumanDate;

$twig = new Twig_Environment($loader);
$twig->addExtension(new HumanDateExtension(new HumanDate()));
```

You can find more information about registering extensions in the [Twig documentation](http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension).

Changelog
---------

[](#changelog)

### Version 0.1 (14 May 2014)

[](#version-01-14-may-2014)

- Initial version (ported from `BraincraftedHumanDateBundle`)

Authors
-------

[](#authors)

- [Florian Eckerstorfer](http://florian.ec) ([Twitter](http://twitter.com/Florian_)) [![Support Florian](https://camo.githubusercontent.com/5d8be0f60738f32a93128d94dec1fa7ee75e6dce71b110ec7fbbf6d217c0a150/687474703a2f2f696d672e736869656c64732e696f2f6769747469702f666c6f7269616e65636b657273746f726665722e737667)](https://www.gittip.com/FlorianEckerstorfer/)

License
-------

[](#license)

The MIT License (MIT) Copyright (c) 2012 Florian Eckerstorfer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

4377d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a80f9fc61cd3a7d7779e8f120b458ca4d18fdd885d719bb77d3379b96bf714d9?d=identicon)[florianeckerstorfer](/maintainers/florianeckerstorfer)

---

Top Contributors

[![c33s](https://avatars.githubusercontent.com/u/649209?v=4)](https://github.com/c33s "c33s (1 commits)")[![florianeckerstorfer](https://avatars.githubusercontent.com/u/149201?v=4)](https://github.com/florianeckerstorfer "florianeckerstorfer (1 commits)")[![kristoftorfs](https://avatars.githubusercontent.com/u/8181149?v=4)](https://github.com/kristoftorfs "kristoftorfs (1 commits)")

---

Tags

datetransformerhuman date

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cocur-human-date/health.svg)

```
[![Health](https://phpackages.com/badges/cocur-human-date/health.svg)](https://phpackages.com/packages/cocur-human-date)
```

###  Alternatives

[cakephp/chronos

A simple API extension for DateTime.

1.4k47.7M119](/packages/cakephp-chronos)[nesbot/carbon

An API extension for DateTime that supports 281 different languages.

169661.4M4.8k](/packages/nesbot-carbon)[azuyalabs/yasumi

The easy PHP Library for calculating holidays

1.1k11.4M25](/packages/azuyalabs-yasumi)[carbonphp/carbon-doctrine-types

Types to use Carbon in Doctrine

213220.4M8](/packages/carbonphp-carbon-doctrine-types)[rlanvin/php-rrule

Lightweight and fast recurrence rules for PHP (RFC 5545)

69810.6M39](/packages/rlanvin-php-rrule)

PHPackages © 2026

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