homepage Welcome to WebmasterWorld Guest from 54.166.108.167
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Convert Rss pubDate To ISO8601 Format
doubleJ




msg:4353286
 10:02 pm on Aug 18, 2011 (gmt 0)

I just figured out how convert my rss pubDate into the iso8601 format that the html5 time tag requires.
I'm sure there's a better way to do it, but it works. I don't really have a grasp on mktime, yet.
Feel free to show me the better ways to do it. I'll try and figure it out.
Please forgive if this code, as pasted, doesn't work correctly. It's snipped from a much larger script that does.
Hehehe...
JJ

<?php
$rss = new SimpleXMLElement("http://$fqdn_mlm/rss/FreeDownloads.xml", null, true);
foreach($rss->xpath("channel/item") as $item) {
$seriesdate = substr($item->pubDate,0,-13);
$iso8601 = explode(" ", $seriesdate);
if ($iso8601[2] == "Jan") $iso8601[2] = "01";
if ($iso8601[2] == "Feb") $iso8601[2] = "02";
if ($iso8601[2] == "Mar") $iso8601[2] = "03";
if ($iso8601[2] == "Apr") $iso8601[2] = "04";
if ($iso8601[2] == "May") $iso8601[2] = "05";
if ($iso8601[2] == "Jun") $iso8601[2] = "06";
if ($iso8601[2] == "Jul") $iso8601[2] = "07";
if ($iso8601[2] == "Aug") $iso8601[2] = "08";
if ($iso8601[2] == "Sep") $iso8601[2] = "09";
if ($iso8601[2] == "Oct") $iso8601[2] = "10";
if ($iso8601[2] == "Nov") $iso8601[2] = "11";
if ($iso8601[2] == "Dec") $iso8601[2] = "12";
echo "<time pubDate datetime="$iso8601[3]-$iso8601[2]-$iso8601[1]">$seriesdate</time>";
}
?>

 

g1smd




msg:4353291
 10:40 pm on Aug 18, 2011 (gmt 0)

Rather than 12 IF statements, I would load the data into an array and use that for the comparison.

doubleJ




msg:4353331
 1:56 am on Aug 19, 2011 (gmt 0)

But wouldn't you still need 12 ifs to compare $iso8601[2] and $array[0, 1, 2, 3, etc...]?
JJ

g1smd




msg:4353386
 7:30 am on Aug 19, 2011 (gmt 0)

Not if you use:
Array ( "Jan" => "01", "Feb" => "02", ...
astupidname




msg:4353389
 7:49 am on Aug 19, 2011 (gmt 0)

Why not just use date() and strtotime():
<?php
$rss = new SimpleXMLElement("http://$fqdn_mlm/rss/FreeDownloads.xml", null, true);
foreach($rss->xpath("channel/item") as $item) {
$iso8601 = date('Y-m-d', strtotime(substr($item->pubDate, 0, -13)));
echo "<time>$iso8601</time>";
}
?>

doubleJ




msg:4353467
 2:37 pm on Aug 19, 2011 (gmt 0)

Hehehe...
Because I didn't know about that.
I figured there was some way to use date(), but I could only figure out how to do stuff with today's date (not a string of characters).
I've not used strtotime, before. I have seen it, though.
JJ

doubleJ




msg:4353469
 2:52 pm on Aug 19, 2011 (gmt 0)


$iso8601 = date('c', strtotime($item->pubDate));

Wow, now that was the easy way to do it.
It's amazing how much difference a point in the right direction can make.
Thanks...
JJ

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved