Formatting Twitter’s Date/Time With PHP

Posted: August 18th, 2010 | Author: | Filed under: Tips/Tutorials
Tags: , , , , ,

This should save you a little googling. When you look at the XML that the Twitter API gives you, each tweet has a ‘created_at‘ timestamp.

It looks like this: Wed Aug 18 18:22:29 +0000 2010. Not too friendly, but it’s really simple to convert it to a unix timestamp, then format it with PHP’s date function.

Step 1: Make the unix timestamp with strtotime(). Now it’s turned into 1282155749, which is just what we need even though it looks worse.

Step 2: Format with date(). ‘l M j \- g:ia‘ turns it into ‘Wednesday Aug 18 – 12:22pm‘. You have total control of how it looks thanks to that date() function of PHP.

I pretty much dumped it all into a single line on one of the things I was working on. This is after I retrieved the XML into a variable called $xml.

echo date("l M j \- g:ia",strtotime($xml->status[0]->created_at));
Last Modified: August 18th, 2010

9 Comments »