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?
JavaScript can parse that date if you remove the
+0000
from the string: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:
Here is date format for Twitter API:
Javascript. As @Andy pointed out, is going to be a bitch when it comes to IE. So it's best to rely 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:
In PHP you can use the date functions or the DateTime class to do the same (available since PHP 5.2.0):
strtotime("dateString");
gets it into the native PHP date format, then you can work with thedate()
function to get it printed out how you'd like it.Cross-browser, time-zone-aware parsing via JavaScript:
Tested on IE, Firefox, Safari, Chrome and Opera.