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)
%>

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in General
8 comments on “PHP & Javascript & ASP: Format ATOM Date
  1. TK says:

    Thanks for the code! But can you please explain how the PHP code can be used? I am very new to ATOM, but I am trying to display it on a website and I really, really, hate the date format. Plus, I can’t figure out where and how to put the PHP code you provided above.

    Thanks in advance.

  2. Chrissy says:

    Hey TK,
    Here is an example of how you can parse ATOM XML in PHP.

    Hope that helps,
    Chrissy

  3. Maltpress says:

    Thank you – struggling with learning PHP and trying to parse my blog on my website. Had it all working except the date and stumbled on this via Google. With a bit of thought, a tiny hint of swearing, and a little sweat, I’ve not only got it working, I’ve also learnt a lot more about PHP…

  4. Deano says:

    ahh thanks!!

  5. Jamie says:

    Thank you so much for your help! This is exactly what I’ve been looking for! It took me a while to find it too, I guess I should’ve typed ‘php format atom date’ into the search and then it would’ve been easier for me. I was trying things like php date help!!!!!!!!!!

    :)

  6. Duncan says:

    Thank you for saving me time and effort!

  7. Timmy says:

    Thanks. Perfect.

  8. Philip says:

    you can now use the shorter method of:

    echo date( DateTime::RFC822, strtotime(‘2009-05-12T16:46:21.000Z’) );

    For constants other than DateTime::RFC822 see:
    http://uk2.php.net/manual/en/class.datetime.php

1 Pings/Trackbacks for "PHP & Javascript & ASP: Format ATOM Date"
  1. […] netnerds.net has a great post on how to convert an ATOM date to a normal PHP date. I found it very useful as I was trying to do this for my sites http://www.stormwarriorsmovie.com and http://www.roguebargains.com and it worked great! […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*