views:

795

answers:

6

I'm using the twitter API to return a list of status updates and the times they were created. It's returning the creation date in the following format:

Fri Apr 09 12:53:54 +0000 2010

What's the simplest way (with PHP or Javascript) to format this like 09-04-2010?

+7  A: 

strtotime("dateString"); gets it into the native PHP date format, then you can work with the date() function to get it printed out how you'd like it.

Alex Mcp
That sounds good thanks, $time = strtotime($phrase->created_at); echo $time;is giving me it like this... 1270817634. How do I use the date() function with that?
Chris Armstrong
Ah wait, got it working. date("jS F Y", $time) gives me 9th April 2010.Thanks!
Chris Armstrong
Yeah, the date parsing in PHP is pretty well formed. Note that strtotime will accept ALMOST anything you throw into it. It's expensive but quite powerful.
Alex Mcp
+1  A: 

Javascript. As @Andy pointed out, is going to be a bitch when it comes to IE. So it's best to reply on a library that does it consistently. DateJS seems like a nice library.

Once the library is added, you would parse and format it as:

var date = Date.parse("Fri Apr 09 12:53:54 +0000 2010");
var formatted = date.toString("dd-MM-yyyy");

In PHP you can use the date functions or the DateTime class to do the same (available since PHP 5.2.0):

$date = new DateTime("Fri Apr 09 12:53:54 +0000 2010");
echo $date->format("d-m-Y"); // 09-04-2010
Anurag
This gives `NaN` in Internet Explorer, although it works in Chrome. IE struggles with the `+0000`.
Andy E
thanks for the tip Andy. it's best to go server side, or use a date parsing library such a datejs
Anurag
There's also a good jQuery PHPDate library available (via google)
Alex Mcp
PHPDate looks like a nice plugin for date handling. Like the way how they setup the "Try me" demos for different formats on the front page.
Anurag
+2  A: 

JavaScript can parse that date if you remove the +0000 from the string:

var dStr = "Fri Apr 09 12:53:54 +0000 2010";
dStr = dStr.replace("+0000 ", "");
var d = new Date(dStr);

Chrome -- and I suspect some other non IE browsers -- can actually parse it with the +0000 present in the string, but you may as well remove it for interoperability.

PHP can parse the date with strtotime:

strtotime("Fri Apr 09 12:53:54 +0000 2010");
Andy E
thanks, I went with the strtotime option. However your Javascript example just answered another question I had, thanks!
Chris Armstrong
However, the original date string is probably in UTC (and hence the +0000). `new Date()` will parse it in the local time zone.
Ates Goral
This gets parsed in the correct time zone: `dStr = dStr.replace("+0000 ", "") + " GMT";`
Ates Goral
@Ates Goral: Thanks for your corrections, I had forgotten that ECMAScript implementations parse dates in the local timezone if one is omitted.
Andy E
@Andy E's head: We're not quite done yet :( Safari doesn't like this but it works fine on IE, FF, Opera and Chrome.
Ates Goral
@Ates Goral: That seems very odd, given that both Safari and Chrome use the same engine (WebKit). Can Safari parse it with the `+0000` in? If it works OK like that then the solution would be to try and create the `Date()` before the replace and fall back to the str replace if that date is NaN.
Andy E
I agree; that's weird. Doing things more explicitly seems to be the only way... I've posted my approach as a separate answer: http://stackoverflow.com/questions/2611415/parsing-twitter-api-datestamp/2766516#2766516
Ates Goral
@Andy E's head: Well Safari and Chrome both use WebKit for rendering HTML but Safari uses SquirrelFish/Nitro and Chrome uses V8 for executing Javascript. That should be the reason for the difference in behaviour.
paracycle
@paracycle: I wasn't aware of that (I thought V8 was part of WebKit), thanks for clarifying. @Ates Goral: +1 for your answer.
Andy E
+7  A: 

Cross-browser, time-zone-aware parsing via JavaScript:

var s = "Fri Apr 09 12:53:54 +0000 2010";

var date = new Date(
    s.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,
        "$1 $2 $4 $3 UTC"));

Tested on IE, Firefox, Safari, Chrome and Opera.

Ates Goral
+1 for that :-)
Andy E
very good mate =)
ludicco
Great answer. Why did Twitter settle on this horrible date format?
brad
I'm wondering the same thing too...
Ates Goral
A: 

Hi,

I used this script to format the date in PHP:

$date = new DateTime("Fri Apr 09 12:53:54 +0000 2010"); echo $date->format("d-m-Y"); // 09-04-2010

Now, how can I get the time-zone do Brazil from this example? Thank you.

Tiago
A: 

Trying to do a similar thing and stumbled in here. Funny thing is, the datestamp I'm getting from Twitter is not Fri 27 Aug 2010 22:00:07 +0000 its Fri, 27 Aug 2010 22:00:07 +0000

Are Twitter really stupid enough to put random commas into their formatted dates? Nothing would surprise me to be honest!!!

jamiet