Yoann Augen
Yoann Augen

Reputation: 2036

Localize dateinterval in Twig

I've a entity method returning a PHP DateInterval between a DateTime property of this entity and now, to know how many time left before this datetime.

I want to display this interval in Twig so I used something like that:

{{ myEntity.getTimeRemaining | date("%D days %H Hours %i Mins ") }}

It's working fine.

Now, how can I localize this format? (and eventually consider the plural) I've found transchoice method but it's seem to be only to localize date, not a interval.

Upvotes: 6

Views: 1055

Answers (1)

Matteo
Matteo

Reputation: 39390

You can use the KnpTimeBundle

I think this bundle can simplify your implementation, so you can directly dump the time difference between now without pass thru a DateInterval, you can simply with the current date:

{# Returns something like "3 minutes ago" #}
{{ time_diff(myEntity.getMyTimeField) }}

This compare with the another date:

{# Returns something like "3 minutes ago" #}
{{ time_diff(myEntity.getMyTimeField , to ) }}

The translation is enabled by default, simply review the translations files or add as you need.

Hope this help

Upvotes: 5

Related Questions