Welcome to WebmasterWorld Guest from 54.147.26.192

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Convert Rss pubDate To ISO8601 Format

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

Junior Member

joined:Feb 15, 2011
posts: 53
votes: 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>";
}
?>
10:40 pm on Aug 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Rather than 12 IF statements, I would load the data into an array and use that for the comparison.
1:56 am on Aug 19, 2011 (gmt 0)

Junior Member

joined:Feb 15, 2011
posts: 53
votes: 0


But wouldn't you still need 12 ifs to compare $iso8601[2] and $array[0, 1, 2, 3, etc...]?
JJ
7:30 am on Aug 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Not if you use:
Array ( "Jan" => "01", "Feb" => "02", ...
7:49 am on Aug 19, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 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>";
}
?>
2:37 pm on Aug 19, 2011 (gmt 0)

Junior Member

joined:Feb 15, 2011
posts: 53
votes: 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
2:52 pm on Aug 19, 2011 (gmt 0)

Junior Member

joined:Feb 15, 2011
posts: 53
votes: 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
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members