Quick Tip: MODX – Modify the date format for the TwitterX output
While working on a MODX client project I used the TwitterX snippet, which uses the new Twitter 1.1 API, to pull in the clients twitter feed to her site. The snippet comes with a default tpl chunk which you should duplicate and modify for your purposes as I have done below to format the tweet feed as you please.
<ul> <li> [[+text:TwitterXFormat]] <a href="http://twitter.com/[[+screen_name]]" target="_blank">[[+created_at:ago]]</a> via [[+source]]</li> </ul>
You definitely must duplicate rather than edit the default tpl, as any updates will overwrite your changes.
Anyway, my client wanted the date format for the tweets to show up as “day month date, year”. For example Sun Jul 7, 2013. I started by removing the “ago” part of the created_at placeholder. This partially got me there:
But now the date is in the format Sun Jul 07 09:24:57 +0000 2013, which is a lot more information than I want. The solution is actually pretty simple. The date format that the Twitter API spits out is the RFC822 format for feeds. How do we convert this to a different format?
The easiest way is to first convert it into a UNIX timestamp and then from the UNIX timestamp into whatever format you desire. At least this is the route I’m taking 🙂
In MODX, we use chained Output modifiers to accomplish our goal. We first convert the RFC822 formatted date into a UNIX timestamp using strtotime, like this:
<ul> <li> [[+text:TwitterXFormat]] <a href="http://twitter.com/[[+screen_name]]" target="_blank">[[+created_at:strtotime]]</a> via [[+source]]</li> </ul>
This gives us:
We then modify that to our desired format using the date modifier and our desired format, like this:
<ul> <li> [[+text:TwitterXFormat]] <a href="http://twitter.com/[[+screen_name]]" target="_blank">[[+created_at:strtotime:date=`%a %b %e, %Y`]]</a> via [[+source]]</li> </ul>
That gives us the desired outcome:
Hope you find this and the links below helpful.
I’ve learned a lot from your posts. Thanks for posting all of the great MODx info.
I seem to have a “love/hate” relationship with that CMS, but for the most part it’s enjoyable to work with.