Tuesday, December 4, 2007

PHP & Javascript & ASP: Format ATOM Date

PHP & Javascript & ASP: Format ATOM Date

I'm surprised this isn't built in.. I had the hardest time finding how to convert ATOM time (found in many RSS feeds) to something human-readable. The cleanest solution I have found thus far is this:

<?php
$atomdate = '2006-04-22T10:00:00.000Z';
$datetime = strtotime(substr($atomdate, 0, 10) . ' ' . substr($atomdate, 11, 8 ));
print date('m.d.y',$datetime);
?>

That outputs "04.22.06" but you can use any of the formats available from php date().

Here is basically the same routine, but this time in Javascript

var atomdate = items[n].getElementsByTagName('published').item(0).firstChild.data;
var itemPubDateDay = atomdate.substr(0, 10);
var itemPubDateTime = atomdate.substr(11, 8 );
print itemPubDateDay;

And this time in ASP

<%
atomdate = "2006-04-22T10:00:00.000Z"
dateandtime = mid(atomdate, 1, 10) & " " & mid(atomdate, 12, 8 )
response.write formatdatetime(dateandtime)
%>