Code Monkey home page Code Monkey logo

knptimebundle's Introduction

knplabs/knp-time-bundle

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

Last edited: {{ post.updatedAt|time_diff }} <!-- Last edited: 1 week ago -->

Event date: {{ event.date|time_diff }} <!-- Event date: in two weeks -->

Read time: {{ post.readTimeInSeconds|duration }} <!-- Read time: 2 minutes -->

Age: {{ user.birthdate|age }} <!-- Age: 30 years old -->

Want to see it used in a screencast ๐ŸŽฅ? Check out SymfonyCasts: https://symfonycasts.com/screencast/symfony-doctrine/ago

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

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

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

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

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

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

./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:

knptimebundle's People

Contributors

weaverryan avatar jrushlow avatar pilot avatar ornicar avatar docteurklein avatar akovalyov avatar herzult avatar kbond avatar m-vo avatar stloyd avatar cvele avatar thomaslandauer avatar chris8934 avatar behram avatar pierstoval avatar mbontemps avatar michalkurzeja avatar umpirsky avatar seb-jean avatar l3pp4rd avatar everzet avatar imanalopher avatar jpgiroux avatar thvd avatar nicolasnssm avatar relo-san avatar gimler avatar ahaaje avatar aleixfargas avatar itavero avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.