knp-time-bundle Documentation

repository·main·Indexed 20 days ago

https://github.com/knplabs/knptimebundle

A Symfony bundle providing human-readable formatting for dates, durations, and ages. Includes Twig filters such as time_diff, ago, duration, and age, as well as the DateTimeFormatter service for use in PHP controllers and services.

Tokens
965
Snippets
5
Records
5
Agent score
21%

What's inside knp-time-bundle

  1. Install knp-time-bundle via Composer

    main

    Install the bundle using Composer. If your project uses Symfony Flex, the bundle will be automatically configured. If not, you must manually enable Knp\Bundle\TimeBundle\KnpTimeBundle in your Symfony configuration.

    composer require knplabs/knp-time-bundle
  2. Use DateTimeFormatter service in PHP

    main

    For use in controllers or services, autowire or inject the Knp\Bundle\TimeBundle\DateTimeFormatter service. It provides methods to format differences, durations, and ages.

    use Knpundle	ime-bundle\
    DateTimeFormatter;
    
    public function yourAction(DateTimeFormatter $dateTimeFormatter)
    {
        $someDate = new \DateTimeImmutable('-2 years');
        $toDate = new \DateTimeImmutable('now');
    
        // Formats difference between two dates (defaults to 'now' if second param is omitted)
        $agoTime = $dateTimeFormatter->formatDiff($someDate, $toDate);
    
        // Formats seconds into a duration string
        $readTime = $dateTimeFormatter->formatDuration(64);
    
        // Formats age (defaults to 'now' if second param is omitted)
        $ageTime = $dateTimeFormatter->formatAge($someDate, $toDate);
    
        return $this->json([
            'published_at' => $agoTime, // 2 years ago
            'read_time' => $readTime,   // 1 minute
            'age' => $ageTime,         // 2 years old
        ]);
    }
  3. Use duration and age formatting in Twig

    main

    Use the duration filter to convert a number of seconds into a human-readable duration, and the age filter to format a birthdate into a human-readable age.

    {# Duration formatting #}
    {{ someDurationInSeconds|duration }} {# 2 minutes #}
    
    {# Age formatting #}
    {{ user.birthdate|age }} {# Age: 30 years old #}
    {{ age(user.birthdate) }} {# Age: 30 years old #}
  4. Use time formatting in Twig

    main

    You can format DateTime objects into friendly "ago" or "until" strings using the time_diff filter or its alias ago. This also works for future dates (e.g., "in 2 months"). You can use either the filter syntax or the function syntax.

    {# Using filters #}
    {{ someDateTimeVariable|time_diff }} {# 2 weeks ago #}
    {{ someDateTimeVariable|ago }} {# 1 second ago #}
    
    {# Using functions #}
    {{ time_diff(someDateTimeVariable) }} {# in 2 months #}
  5. Override the translation locale in Twig

    main

    By default, the bundle uses the current application locale. You can override this for specific calls by passing a locale argument to the time_diff, ago, duration, or age filters.

    {{ someDateTimeVariable|time_diff(locale='es') }}
    {{ someDurationInSeconds|duration(locale='es') }}
    {{ someDateTimeVariable|age(locale='es') }}