Formatting Twitter’s Date/Time With PHP
Posted: August 18th, 2010 | Author: kn00tcn | Filed under: Tips/TutorialsTags: api, formatting, php, time-saver, twitter, xml
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)); |
9 Comments »


Beautiful! Thanks.
Thank you! That’s good tip.
Allstar :) Cheers!!
I used your method but the time is not right, for some reason date(“d/m/Y g:ia”,strtotime($obj[$var]->created_at)); returns time -4 hours from the created_at string. Any thoughts on that?
@Elendurwen not sure… wonder if it’s something to do with your host, you could just add 4 hours to it i guess (or maybe there’s a time zone function)
Thanks ……
You are now required to make use the date.timezone setting or the date_default_timezone_set() function in PHP, so this may need updating slightly.
@Alex i also have to update the retrieval of tweets since the API changed, i found a nice WP plugin so that made me even lazier
For converting it into MySQL Datetime Format
$modified_date = date(“Y-m-d H:i:s”, strtotime($date));