3

I'd like to reopen this question:

The answer was a plugin called Nice Time

But This plugin is depricated and the creator stats that this functionality is acheivable via the date_modify filter.

Yes okay, but how?

I have a event archive where I want to show "This event happend xy days ago".

There is a time_diff filter in twig. But it seems as if this is not activated in craft.

2 Answers 2

6

You could use the diff method to get the number of days between two dates. So for example:

{{ date(entry.dateCreated|date('c')).diff(now).days }} days ago

In my example between today (using now) and the entry.dateCreated.

There is a explanation as to why you need to wrap the first date in the date filter here, which is where I got the answer from!

2
  • How would I print seconds or hours? .seconds does nothing?
    – KSPR
    Commented Mar 14, 2016 at 14:07
  • 1
    Instead of .days, use .format, and then use one of the format chars listed here. So for example to get months use .format('%m'). Commented Mar 14, 2016 at 14:18
3

I have a plugin that adds the time_diff Twig filter to Craft:

https://github.com/mildlygeeky/craft-timediff

There is one possible issue (in either PHP or Craft) around DST, and I will add that to the README.

3
  • Unfortunately that ads a suffix "days ago" which is bad for a german Site :)
    – KSPR
    Commented Mar 14, 2016 at 14:08
  • Hi @KSPR - just added a new version that includes an array option with translated strings where possible, and you could then use the raw output to format the diff however you'd like. :) Commented Mar 14, 2016 at 15:10
  • Nice :) I'll check it out.
    – KSPR
    Commented Mar 15, 2016 at 7:27

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.