<p>Great! Thank you @techgyver</p>

<p>Here is my final code:</p>

<pre><code>{% for tweet in tweets %}
{% set tweetDate = date(tweet.created_at).diff(now).format('%a') %}
    {% if tweetDate == 0 %}
        {% set tweetTimeAgo = 'today' %}
    {% else %}
        {% set tweetTimeAgo = tweetDate ~ ' days ago' %}
    {% endif %}

&lt;div class="tweet-time"&gt;{{ tweetTimeAgo }}&lt;/div&gt;
&lt;div class="tweet-text"&gt;{{ tweet.text|autoLinkTweet }}&lt;/div&gt;
{% endfor %}
</code></pre>

<p>Which either outputs 'today' or '3 days ago' depending on diff since tweet.</p>

CC BY-SA 3.0
2016-09-05T11:14:27.123Z
2016-09-05T11:14:27.123Z
Adam Menczykowski
<p>Further to Adam's answer, and for everyone else who is reading this question, there is a Craft plugin that can do the date calculation logic, as well as providing more readable timestamps:</p>

<p><a href="https://github.com/blue-mantis/BM-Time-Ago-In-Words" rel="nofollow">https://github.com/blue-mantis/BM-Time-Ago-In-Words</a>.</p>

<p>As well as '...days ago' it extends into weeks and months, which may not be <em>as</em> useful in this scenario but could prove more useful for blog/news sections.</p>

CC BY-SA 3.0
2016-09-05T12:43:46.197Z
Thanks that is a great plugin by the sounds of it, improving the functionality of my original question
Adam Menczykowski
CC BY-SA 3.0
2016-09-05T12:42:24.697Z
2016-09-05T12:42:24.697Z
Jamie Wade
<p>You can use the date method for creating a date object. 
e.g.: 
<code>{% set tweetDate = date("Fri Sep 02 14:42:42 +0000 2016") %}</code></p>

CC BY-SA 3.0
2016-09-05T11:04:15.390Z
2016-09-05T11:04:15.390Z
techgyver
<p>I am using the Dukt Twitter plugin and want to display the time since the tweet was created.</p>

<p>The Twitter API returns a string in the format:</p>

<p><code>Fri Sep 02 14:42:42 +0000 2016</code></p>

<p>I can use the date filter on this string to alter the format, but cannot pass this through the <code>diff</code> method that twig exposes. I think I need to create a dateTime object, would this require a plugin to be created for this task or can I achieve this in twig?</p>

<p>A reference post on this forum discusses the <code>diff</code> method: <a href="https://craftcms.stackexchange.com/questions/9137/get-age-from-date-of-birth/9138#9138">Get Age from Date of Birth</a> - however I think I need to have a date object, not a string, to use it. Ideally I'd like to just achieve this in twig as I have never written a plugin before! Thanks</p>

<plugin-dukttwitter>
Twitter plugin - Time since tweet
# Twitter plugin - Time since tweet

(Asked by: username_0 on 2016-09-05)

I am using the Dukt Twitter plugin and want to display the time since the tweet was created.

The Twitter API returns a string in the format:

`Fri Sep 02 14:42:42 +0000 2016`

I can use the date filter on this string to alter the format, but cannot pass this through the `diff` method that twig exposes. I think I need to create a dateTime object, would this require a plugin to be created for this task or can I achieve this in twig?

A reference post on this forum discusses the `diff` method: [Get Age from Date of Birth](https://craftcms.stackexchange.com/questions/9137/get-age-from-date-of-birth/9138#9138) - however I think I need to have a date object, not a string, to use it. Ideally I'd like to just achieve this in twig as I have never written a plugin before! Thanks

**Tags:** plugin-dukttwitter

---

## 3 answers

### Answer by username_0 on 2016-09-05. Score: 3

Great! Thank you @techgyver
Here is my final code:
{% for tweet in tweets %}
{% set tweetDate = date(tweet.created_at).diff(now).format('%a') %}
    {% if tweetDate == 0 %}
        {% set tweetTimeAgo = 'today' %}
    {% else %}
        {% set tweetTimeAgo = tweetDate ~ ' days ago' %}
    {% endif %}

{{ tweetTimeAgo }}
{{ tweet.text|autoLinkTweet }}
{% endfor %}

Which either outputs 'today' or '3 days ago' depending on diff since tweet.


---

### Answer by username_1 on 2016-09-05. Score: 2

Further to Adam's answer, and for everyone else who is reading this question, there is a Craft plugin that can do the date calculation logic, as well as providing more readable timestamps:

<https://github.com/blue-mantis/BM-Time-Ago-In-Words>.

As well as '...days ago' it extends into weeks and months, which may not be *as* useful in this scenario but could prove more useful for blog/news sections.

> **Comments:**
> **username_0**: Thanks that is a great plugin by the sounds of it, improving the functionality of my original question  

---

### Answer by username_2 on 2016-09-05. Score: 2

You can use the date method for creating a date object.
e.g.:
`{% set tweetDate = date("Fri Sep 02 14:42:42 +0000 2016") %}`


---

