6
{%for mat in setQuery %}
   {% set datePost = mat.data_criacao|date('d-m-Y') %}
   {% set today = "now"|date('d-m-Y') %}
   {{today- datePost}}
{% endfor %}

datePost = 17-04-2015 today = 06-05-2015

the example above returns it: -11

2 Answers 2

11

The issue was resolved with the following code:

{% set datePost = mat.data_criacao|date('d-m-Y') %}
{% set today = "now"|date('d-m-Y') %}
{% set difference = date(today).diff(date(datePost))%}
{% set leftDays = difference.days %}
{% if datePost == today %}
      1 day
{% else %}
    {{ leftDays }}
{% endif %}          
1
  • 1
    Thank you! It's great to see a solution that works with just the built-in tools. Personally, I'd have picked this as the accepted answer. Commented Aug 22, 2019 at 5:24
3

You must write your custom twig extension:

You must write a twig function as described here with the following code for make diff via php function:

$calcFrom = $from;

$calcTo = $to;
$now->diff($calcFrom)->format("%a")

And make it available via a Twig extension.

If you are using symfony2 framework You can use the KnpTimeBundle

In the Twig: This compare with the current date:

{# Returns something like "3 minutes ago" #}
{{ time_diff(form) }}

This compare with the another date:

{# Returns something like "3 minutes ago" #}
{{ time_diff(form , to ) }}

Hope this help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.