<p>Check out the <code>DateTimeHelper</code> method <code>timeAgoInWords</code>. It will do what you are after.</p>

<p><a href="https://craftcms.com/classreference/helpers/DateTimeHelper#timeAgoInWords-detail" rel="nofollow noreferrer">https://craftcms.com/classreference/helpers/DateTimeHelper#timeAgoInWords-detail</a></p>

<p>There is even a handy twig filter available for use in your templates.</p>

<p><code>{{ entry.dateCreated|timeAgoInWords }}</code></p>

CC BY-SA 3.0
2017-03-20T23:34:04.583Z
2017-03-20T23:34:04.583Z
Fyrebase
<p>I am trying to print if statement for x time or x day ago for each published entries. For example the entry is posted x minutes ago should display x minutes ago or x hours ago. Same for days and months.</p>

<p>Here is what I have used but not what I am after. </p>

<pre><code>{{ date(category.dateCreated|date('c')).diff(now).format('%a') }}
</code></pre>

<p>But the code I have written does not work.</p>

<pre><code>{% for category in entries %}

{% for time in category %}
&lt;span&gt;{{ date(time.dateCreated|date('c')).diff(now).format('%i') }} minutes ago&lt;/span&gt;

{% else %}
&lt;span&gt;{{ date(time.dateCreated|date('c')).diff(now).format('%h') }} hours ago&lt;/span&gt;

{% else %}
&lt;span&gt;{{ date(time.dateCreated|date('c')).diff(now).format('%d') }} days  agos&lt;/span&gt;
{% endfor %}
{% endfor %}
</code></pre>

<p>The above code is too long or is there a better way to check whether the entry is posted min, hours, or days ago.</p>

CC BY-SA 3.0
2017-03-20T03:25:09.857Z
Check out the `DateTimeHelper` method `timeAgoInWords`. It will do what you are after.

https://craftcms.com/classreference/helpers/DateTimeHelper#timeAgoInWords-detail
Fyrebase
CC BY-SA 3.0
2017-03-20T08:27:44.037Z
@Fyrebase Well I learned something today. You should post this as an answer.
Stephen Lewis
CC BY-SA 4.0
2018-06-04T15:02:18.280Z
Any idea what the Craft 3 version of `timeAgoInWords()` is?
Sandwich
<templating><datetime>
Display X minute / hours / days in entries
# Display X minute / hours / days in entries

(Asked by: username_0 on 2017-03-19)

I am trying to print if statement for x time or x day ago for each published entries. For example the entry is posted x minutes ago should display x minutes ago or x hours ago. Same for days and months.
Here is what I have used but not what I am after. 
{{ date(category.dateCreated|date('c')).diff(now).format('%a') }}

But the code I have written does not work.
{% for category in entries %}

{% for time in category %}
{{ date(time.dateCreated|date('c')).diff(now).format('%i') }} minutes ago

{% else %}
{{ date(time.dateCreated|date('c')).diff(now).format('%h') }} hours ago

{% else %}
{{ date(time.dateCreated|date('c')).diff(now).format('%d') }} days  agos
{% endfor %}
{% endfor %}

The above code is too long or is there a better way to check whether the entry is posted min, hours, or days ago.

**Tags:** templating, datetime
> **Comments:**
> **username_1**: Check out the `DateTimeHelper` method `timeAgoInWords`. It will do what you are after.  https://craftcms.com/classreference/helpers/DateTimeHelper#timeAgoInWords-detail  
> **username_2**: @Fyrebase Well I learned something today. You should post this as an answer.  
> **username_3**: Any idea what the Craft 3 version of `timeAgoInWords()` is?  

---

## 1 answers

### Answer by username_1 on 2017-03-20. Score: 4

Check out the `DateTimeHelper` method `timeAgoInWords`. It will do what you are after.

<https://craftcms.com/classreference/helpers/DateTimeHelper#timeAgoInWords-detail>

There is even a handy twig filter available for use in your templates.

`{{ entry.dateCreated|timeAgoInWords }}`


---

