PHPackages                             knplabs/knp-time-bundle - 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. knplabs/knp-time-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

knplabs/knp-time-bundle
=======================

Making your dates and durations look sensible and descriptive

2.5.0(5mo ago)6308.9M↓19%104[1 issues](https://github.com/KnpLabs/KnpTimeBundle/issues)[4 PRs](https://github.com/KnpLabs/KnpTimeBundle/pulls)20MITPHPPHP &gt;=8.1CI passing

Since Jan 11Pushed 5mo ago31 watchersCompare

[ Source](https://github.com/KnpLabs/KnpTimeBundle)[ Packagist](https://packagist.org/packages/knplabs/knp-time-bundle)[ Docs](https://github.com/KnpLabs/KnpTimeBundle)[ RSS](/packages/knplabs-knp-time-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (42)Used By (20)

knplabs/knp-time-bundle
=======================

[](#knplabsknp-time-bundle)

Friendly *ago*/*until* dates ("5 minutes ago" or "in 5 minutes") and *durations* ("2 mins")!

```
Last edited: {{ post.updatedAt|time_diff }}

Event date: {{ event.date|time_diff }}

Read time: {{ post.readTimeInSeconds|duration }}

Age: {{ user.birthdate|age }}
```

Want to see it used in a screencast 🎥? Check out SymfonyCasts:

The formatted date/duration can be translated into any language, and many are supported out of the box.

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

[](#installation)

Use Composer to install the library:

```
composer require knplabs/knp-time-bundle

```

Woo! You did it! Assuming your project uses Symfony Flex, the bundle should be configured and ready to go. If not, you can enable `Knp\Bundle\TimeBundle\KnpTimeBundle` manually.

Usage
-----

[](#usage)

### Twig

[](#twig)

Time formatting:

```
{{ someDateTimeVariable|time_diff }} {# 2 weeks ago #}

{# |ago is an alias for |time_diff #}
{{ someDateTimeVariable|ago }} {# 1 second ago #}

{# ... or use the equivalent function: #}
{{ time_diff(someDateTimeVariable) }} {# in 2 months #}
```

> **Note**: the `time_diff` filter/function and `ago` alias works fine for dates in the future, too.

Duration formatting:

```
{{ someDurationInSeconds|duration }} {# 2 minutes #}
```

Age formatting:

```
{# with filter: #}
Age: {{ user.birthdate|age }} {# Age: 30 years old #}

{# ... or use the equivalent function: #}
Age: {{ age(user.birthdate) }} {# Age: 30 years old #}
```

### Service

[](#service)

You can also format dates and durations in your services/controllers by autowiring/injecting the `Knp\Bundle\TimeBundle\DateTimeFormatter` service:

```
use Knp\Bundle\TimeBundle\DateTimeFormatter;
// ...

public function yourAction(DateTimeFormatter $dateTimeFormatter)
{
    $someDate = new \DateTimeImmutable('-2 years'); // or $entity->publishedDate()
    $toDate = new \DateTimeImmutable('now');

    $agoTime = $dateTimeFormatter->formatDiff($someDate, $toDate); // $toDate parameter is optional and defaults to "now"

    $readTime = $dateTimeFormatter->formatDuration(64); // or $entity->readTimeInSeconds()

    $ageTime = $dateTimeFormatter->formatAge($someDate, $toDate); // $toDate parameter is optional and defaults to "now"

    return $this->json([
        //  ...
        'published_at' => $agoTime, // 2 years ago
        'read_time' => $readTime, // 1 minute
        // ...
    ]);
}
```

Controlling the Translation Locale
----------------------------------

[](#controlling-the-translation-locale)

The bundle will automatically use the current locale when translating the "time\_diff" ("ago") and "duration" messages. However, you can override the locale:

```
{{ someDateTimeVariable|time_diff(locale='es') }}

{{ someDurationInSeconds|duration(locale='es') }}

{{ someDateTimeVariable|age(locale='es') }}
```

Tests
-----

[](#tests)

If you want to run tests, please check that you have installed dev dependencies.

```
./vendor/bin/phpunit
```

Maintainers
-----------

[](#maintainers)

Anyone can contribute to this repository (and it's warmly welcomed!). The following people maintain and can merge into this library:

- [@akovalyov](https://github.com/akovalyov)
- [@weaverryan](https://github.com/weaverryan)
- [@NicolasNSSM](https://github.com/NicolasNSSM)

###  Health Score

70

—

ExcellentBetter than 100% of packages

Maintenance71

Regular maintenance activity

Popularity68

Solid adoption and visibility

Community45

Growing community involvement

Maturity84

Battle-tested with a long release history

 Bus Factor3

3 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 ~117 days

Recently: every ~193 days

Total

41

Last Release

161d ago

Major Versions

v1.20.0 → v2.0.02023-07-31

PHP version history (8 changes)v1.0PHP &gt;=5.3.2

v1.2PHP &gt;=5.3.3

1.7.0PHP &gt;=5.5

v1.10.0PHP &gt;=5.6

v1.11.0PHP ^7.1.3

v1.14.0PHP &gt;=7.1.3

v1.16.0PHP &gt;=7.2.5

v2.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/202732?v=4)[KNP Labs](/maintainers/KnpLabs)[@KnpLabs](https://github.com/KnpLabs)

---

Top Contributors

[![weaverryan](https://avatars.githubusercontent.com/u/121003?v=4)](https://github.com/weaverryan "weaverryan (89 commits)")[![jrushlow](https://avatars.githubusercontent.com/u/40327885?v=4)](https://github.com/jrushlow "jrushlow (31 commits)")[![pilot](https://avatars.githubusercontent.com/u/28564?v=4)](https://github.com/pilot "pilot (25 commits)")[![ornicar](https://avatars.githubusercontent.com/u/140370?v=4)](https://github.com/ornicar "ornicar (20 commits)")[![docteurklein](https://avatars.githubusercontent.com/u/109846?v=4)](https://github.com/docteurklein "docteurklein (13 commits)")[![akovalyov](https://avatars.githubusercontent.com/u/2339101?v=4)](https://github.com/akovalyov "akovalyov (13 commits)")[![Herzult](https://avatars.githubusercontent.com/u/177588?v=4)](https://github.com/Herzult "Herzult (10 commits)")[![stloyd](https://avatars.githubusercontent.com/u/67402?v=4)](https://github.com/stloyd "stloyd (8 commits)")[![m-vo](https://avatars.githubusercontent.com/u/5305677?v=4)](https://github.com/m-vo "m-vo (8 commits)")[![kbond](https://avatars.githubusercontent.com/u/127811?v=4)](https://github.com/kbond "kbond (8 commits)")[![cvele](https://avatars.githubusercontent.com/u/644800?v=4)](https://github.com/cvele "cvele (6 commits)")[![behram](https://avatars.githubusercontent.com/u/4572080?v=4)](https://github.com/behram "behram (4 commits)")[![Chris8934](https://avatars.githubusercontent.com/u/44963939?v=4)](https://github.com/Chris8934 "Chris8934 (4 commits)")[![ThomasLandauer](https://avatars.githubusercontent.com/u/1054469?v=4)](https://github.com/ThomasLandauer "ThomasLandauer (4 commits)")[![l3pp4rd](https://avatars.githubusercontent.com/u/132389?v=4)](https://github.com/l3pp4rd "l3pp4rd (3 commits)")[![mbontemps](https://avatars.githubusercontent.com/u/231249?v=4)](https://github.com/mbontemps "mbontemps (3 commits)")[![michalkurzeja](https://avatars.githubusercontent.com/u/5285809?v=4)](https://github.com/michalkurzeja "michalkurzeja (3 commits)")[![Pierstoval](https://avatars.githubusercontent.com/u/3369266?v=4)](https://github.com/Pierstoval "Pierstoval (3 commits)")[![seb-jean](https://avatars.githubusercontent.com/u/12116264?v=4)](https://github.com/seb-jean "seb-jean (3 commits)")[![umpirsky](https://avatars.githubusercontent.com/u/208957?v=4)](https://github.com/umpirsky "umpirsky (3 commits)")

---

Tags

datephptimebundleknpknplabstimedatedurationdescriptive time

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/knplabs-knp-time-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/knplabs-knp-time-bundle/health.svg)](https://phpackages.com/packages/knplabs-knp-time-bundle)
```

###  Alternatives

[brick/date-time

Date and time library

3623.3M61](/packages/brick-date-time)[pentatrion/vite-bundle

Vite integration for your Symfony app

2755.3M13](/packages/pentatrion-vite-bundle)[symfony/ux-toggle-password

Toggle visibility of password inputs for Symfony Forms

26508.0k5](/packages/symfony-ux-toggle-password)[spomky-labs/pwa-bundle

Progressive Web App Manifest Generator Bundle for Symfony.

6144.4k1](/packages/spomky-labs-pwa-bundle)[netgen/layouts-core

Netgen Layouts enables you to build and manage complex web pages in a simpler way and with less coding. This is the core of Netgen Layouts, its heart and soul.

3689.4k10](/packages/netgen-layouts-core)[netgen/content-browser

Netgen Content Browser is a Symfony bundle that provides an interface which selects items from any kind of backend and returns the IDs of selected items back to the calling code.

14112.1k8](/packages/netgen-content-browser)

PHPackages © 2026

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