Q:

Twig, subtract 2 dates

How can I subtract two dates with twig ?
I have an ending date and a begining date and I want the difference between both.
For example for both date "2015-02-20" and "2015-02-13" I should obtain "7".
How can I do that ?
Thanks for help

A:

First Solution (recommended)  "Use an existing library":
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 ) }}

Second Solution  "Do it yourself":
Make diff via php function:
$calcFrom = $from;

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

And make it available via a Twig extension or directly in an helper method in the entity.
Another possible solution is to write register a custom DQL Function to do the work in the repository 
Hope this help

