GithubHelp home page GithubHelp logo

time-ago-in-words's Introduction

Time ago in words Twig extension

This is a Twig extension for Symfony2 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 Symfony2

  1. Update your composer.json
{
	"require": {
		"salavert/time-ago-in-words": "1.*"
	}
}

or use composer's require command:

composer require salavert/time-ago-in-words:1.*
  1. 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

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": 

time-ago-in-words's People

Contributors

salavert avatar stevelacey avatar toomaie avatar micronax avatar adam187 avatar vhpoet avatar

Watchers

James Cloos avatar  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.