2017 © Pedro Peláez
 

symfony-bundle knp-time-bundle

Knplabs time bundle makes your dates look sensible and descriptive

image

knplabs/knp-time-bundle

Knplabs time bundle makes your dates look sensible and descriptive

  • Tuesday, December 26, 2017
  • by Knplabs
  • Repository
  • 31 Watchers
  • 280 Stars
  • 900,096 Installations
  • PHP
  • 22 Dependents
  • 0 Suggesters
  • 72 Forks
  • 5 Open issues
  • 20 Versions
  • 7 % Grown

The README.md

knplabs/knp-time-bundle

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

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: https://symfonycasts.com/screencast/symfony-doctrine/ago, (*2)

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

Installation

Use Composer to install the library:, (*4)

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., (*5)

Usage

Twig

Time formatting:, (*6)

{{ 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., (*7)

Duration formatting:, (*8)

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

Age formatting:, (*9)

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

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

Service

You can also format dates and durations in your services/controllers by autowiring/injecting the Knp\Bundle\TimeBundle\DateTimeFormatter service:, (*10)

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

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

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

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

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

Tests

If you want to run tests, please check that you have installed dev dependencies., (*12)

./vendor/bin/phpunit

Maintainers

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

The Versions

26/12 2017

dev-master

9999999-dev http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

26/12 2017

1.8.0

1.8.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

27/10 2017

1.7.5

1.7.5.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

04/08 2017

dev-doc/update-maintainers

dev-doc/update-maintainers http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

12/07 2017

1.7.4

1.7.4.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

04/03 2017

1.7.3

1.7.3.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

02/02 2017

1.7.2

1.7.2.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

20/01 2017

1.7.1

1.7.1.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

22/10 2016

1.7.0

1.7.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

10/05 2016

1.6.1

1.6.1.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

22/04 2016

1.6.0

1.6.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

24/11 2015

1.5.3

1.5.3.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

12/11 2015

1.5.2

1.5.2.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

The Development Requires

bundle date time knplabs knp descriptive time

11/11 2015

1.5.1

1.5.1.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time

07/10 2015

1.5.0

1.5.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time

17/09 2015

1.4.0

1.4.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time

12/12 2013

v1.3

1.3.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time

15/11 2013

v1.2

1.2.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time

11/01 2013

v1.1

1.1.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time

11/01 2013

v1.0

1.0.0.0 http://github.com/KnpLabs/KnpTimeBundle

Knplabs time bundle makes your dates look sensible and descriptive

  Sources   Download

MIT

The Requires

 

bundle date time knplabs knp descriptive time