Welcome to WebmasterWorld Guest from 54.145.15.213

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Convert Rss pubDate To ISO8601 Format

     

doubleJ

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

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

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



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

doubleJ

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

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

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



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

astupidname

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

5+ Year Member



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

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

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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month