tags:

views:

30

answers:

3

Hi. I'm trying to get twitter statuses displaying on my blog, however I cannot get the time each status is created at to display the way in which I desire. Here is how it is being printed now:

Thu Aug 05 12:36:20 +0000 2010

However I would like it to be displayed like this:

54 days ago

How can I manage this with PHP preg_replace?

Also at the moment I am using the twitter API to get the statuses. Is it better to use this method or an RSS feed? I would appreciate if anyone could help me out. Thanks

A: 

You don't need to manually parse the date, it's already in an understandable format. If you run it through strtotime it will return a timestamp that you can work with.

The concept of displaying time as you want is called "fuzzy time", and you can find a really code post on it here.

ryeguy
A: 

You won't be able to do this with preg_replace() alone. You need to do some temporal comparisons to create a human readable timestamp. Check out this post. Also keep in mind that Twitter responses will be GMT.

As far as the API vs. RSS, this is really up to you. Both responses have to be parsed. There is arguably more overhead with the API now that Twitter only supports OAuth. Although there are several PHP libraries available. If you only want to display statuses, I'd go with the RSS.

Jason McCreary
How can I do it via RSS? I cannot seem to find a tutorial anywhere.
Matthew Ruddy
+1  A: 

I would really recommend writing it out to the page as "Aug 5 2010" (or however you want it to appear). That way you only need to write it out once ever, not once per day. But also write as a GMT timestamp in a way that JS can read it but people that have JS turned off cannot see it. Then, once you've got your page displaying things correctly, use a JS script to loop through the tags and replace the dates with the friendly text you want. Example:

<span class="dateToBeReplaced" title="Thu Aug 05 2010 12:36:20 GMT+0000">Aug 05 2010</span>

The JS would look something like this (uses jQuery): http://jsfiddle.net/JxTLt/4/

JS is a little more finicky about date formats than PHP, so you pretty much should stick the the format above. Use strtotime() to handle the formatting and time zone conversion.

Andrew