Coder Social home page Coder Social logo

time-ago-in-words's Introduction

Time ago in words Twig extension

Packagist Build Status

This is a Twig extension for Symfony Framework where you can easily convert a datetime/timestamp to a distance of time in words.

By example

{{ user.lastLogin | time_ago_in_words }}

Outputs 3 days ago

Installation for Symfony

  1. Update your composer.json

    • Symfony 2/3 with Twig 1/2

      "require": {
          "salavert/time-ago-in-words": "1.*"
      }

      or use composer's require command: composer require salavert/time-ago-in-words:1.*

    • Symfony 4 with Twig 2/3

      "require": {
          "salavert/time-ago-in-words": "2.*"
      }

      or use composer's require command: composer require salavert/time-ago-in-words:2.*

  2. Register an Extension as a Service

Now you must let the Service Container know about your newly created Twig Extension:

YAML:

# app/config/config.yml
services:
    salavert.twig.time_ago:
        class: Salavert\Twig\Extension\TimeAgoExtension
        arguments: ["@translator"]
        tags:
            - { name: twig.extension }

XML:

# or into your bundle src\Acme\AcmeBundle\Resources\config\services.xml
<service id="salavert.twig.time_ago" class="Salavert\Twig\Extension\TimeAgoExtension">
    <tag name="twig.extension" />
    <argument type="service" id="translator" />
</service>

Usage

To display distance of time in words between a date and current date:

{{ message.created | time_ago_in_words }}

To display distance of time between two custom dates you should use

{{ message.created | distance_of_time_in_words(message.updated) }}

You also have two available options, for both time_ago_in_words & distance_of_time_in_words filters

  • include_seconds (boolean) if you need more detailed seconds approximations if time is less than a minute
  • include_months (boolean) if you want days to be approximated in months if time is greater than 31 days.

Thus, if you want to have the months approximation but not the seconds one, you should use:

{{ message.created | time_ago_in_words(false, true) }}

Translations

Add the following translations to your

  • \app\Resources\translations\messages.locale.yml or
  • \translations\messages.locale.yml

This is a translation to spanish:

# Time ago in words - Twig Extension
less than %seconds seconds ago: hace menos de %seconds segundos
half a minute ago: hace medio minuto
less than a minute ago: hace menos de un minuto
1 minute ago: hace 1 minuto
%minutes minutes ago: hace %minutes minutos
about 1 hour ago: hace casi 1 hora
about %hours hours ago: hace %hours horas
1 day ago: hace 1 día
%days days ago: hace %days días
"{1} 1 month ago |]1,Inf[ %months months ago": "{1} hace un mes |]1,Inf[ hace %months meses"
"{1} 1 year ago |]1,Inf[ %years years ago":  "hace un año |]1,Inf[ Hace %years años"

In the same case, for future:

# Time ago in words - Twig Extension
in less than %seconds seconds: en menos de %seconds segundos
in half a minute: en medio minuto
in less than a minute: en menos de un minuto
in 1 minute: en 1 minuto
in %minutes minutes: en %minutes minutos
in about 1 hour: dentro de casi 1 hora
in about %hours hours: en %hours horas
in 1 day: en 1 día
in %days days: en %days días

Testing

To launch all tests first make sure dependecies are met with composer and run:

./vendor/phpunit/phpunit/phpunit

time-ago-in-words's People

Contributors

adam187 avatar aldor007 avatar benjamindulau avatar christianvermeulen avatar dantleech avatar davidino avatar desarrolla2 avatar micronax avatar nixilla avatar peter279k avatar robertobermejo avatar salavert avatar slejnej avatar stevelacey avatar toomaie avatar vhpoet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

time-ago-in-words's Issues

Composer/packagist support

Hi,

Any chance you can add composer/packagist support for this. I'd issue a PR but it's up to you how you want to define your own namespace.

Thanks,
Dean

Support for different translation domain

It would be nice to be able to use a different translation domain for the time_ago_in_words filter. My use case is that I want to have different (shorter) translations on mobile. If this is something you're willing to add I can work on a PR for this.

Feature Request: include Years ago

Is it at all possible to include years. Ie: If days > 365 show 1 year etc etc etc etc.

This would be helpful because 43 months means nothing but 3 years does mean something.

Thanks :)

Years ago returns incorrectly

On the latest version on master, using a time greater than one year with include_months = true, it returns "%years years ago"

The variable looks like it is not set on line 137 in https://github.com/salavert/time-ago-in-words/blob/master/src/Salavert/Twig/Extension/TimeAgoExtension.php

array('%months' => round($distance_in_days/365) should be changed to %years

Translation don't run

Hi,

I'm trying to use this bundle with PT-BR (Brazilian Portuguese) translation, but return english yet.

Translation path: app/Resources/translations/messages.pt_BR.yml

# Time ago in words - Twig Extension @see https://github.com/salavert/time-ago-in-words
less than %seconds seconds ago: há menos de %seconds segundos
half a minute ago: há meio minuto
less than a minute ago: há menos de 1 minuto
1 minute ago: há 1 minuto
%minutes minutes ago: há %minutes minutos
about 1 hour ago: há quase 1 hora
about %hours hours ago: há %hours horas
1 day ago: há 1 dia
%days days ago: há %days dias
"{1} 1 month ago |]1,Inf[ %months months ago": "{1} há 1 mês |]1,Inf[ há %months meses"
"{1} 1 year ago |]1,Inf[ %years years ago":  "há 1 ano |]1,Inf[ há %years anos"
#app/config/parameters.yml
parameters:
    #...
    locale: pt_BR
#app/config/config.yml
services:
    salavert.twig.time_ago:
        class: Salavert\Twig\Extension\TimeAgoExtension
        arguments: [@translator]
        tags:
        - { name: twig.extension }

What's the problem?

Thank's!

Missing dependencies?

I was trying to use this outside of a Symfony application and received this message as there are missing dependencies:

PHP Fatal error:  Class 'Symfony\\Component\\Form\\Extension\\Core\\DataTransformer\\DateTimeToStringTransformer' not found in /private/var/www/phpctf/vendor/salavert/time-ago-in-words/src/Salavert/Twig/Extension/TimeAgoExtension.php on line 65

Is this something that can be included or does it open a whole can of worms for other dependencies?

DOCS: composer and "months ago"

Update docs (update with composer.json) with:
"salavert/time-ago-in-words": "1."
instead of:
"salavert/time-ago-in-words:1.
"

And remove "include_months" description if you have removed it from code (why?).

Show time ago in months?

Hello!!

You can show instead of days, months, for example the months that published one post if over 30 days, and if over 365 days since that shows a year and one day.

Thanks

Miss Tranlator Type

Hi,

I have updated tonight and found some error :

 [ErrorException]
  Catchable Fatal Error: Argument 1 passed to Salavert\Twig\Extension\TimeAgoExtension::__construct() must be an instance of Symfony\Component\Translation\IdentityTranslator,
  instance of Symfony\Bundle\FrameworkBundle\Translation\Translator given, called in /Users/admin/ds-2015/app/cache/de_/ap_DevDebugProjectContainer.php on line 2227 and define
  d in /Users/admin/ds-2015/vendor/salavert/time-ago-in-words/src/Salavert/Twig/Extension/TimeAgoExtension.php line 21     

I have notice you have miss data type Translator on this section :

//use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Translation\Translator;

class TimeAgoExtension extends \Twig_Extension
{
    /**
     * @var IdentityTranslator
     */
    protected $translator;

    /**
     * Constructor method
     *
     * @param IdentityTranslator $translator
     */
    public function __construct(Translator $translator)

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.