Jump to content

RSS pubDate to time


Jesper

Recommended Posts

Hey everyone,

 

While I was working on an RSS script, I ran into a problem. How can I turn the pubDate from RSS feeds (format: "Fri, 13 Mar 2009 11:40:02 +0000") into a time (like mktime() does). I've tried strtotime() already but it didn't working, and I was wondering if there was an efficient way.

 

 

Thanks in advance,

 

Jesper

Link to comment
https://forums.phpfreaks.com/topic/149228-rss-pubdate-to-time/
Share on other sites

You are going to have to build a whole new function for this one.

 

Something like:

<?php
function pub2stamp($pubDate) {
  $months = array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"); //We need something to reference the months.
  $pubDate = str_replace(" ", "", $pubDate); //Get rid of spaces
  $pubDate = str_replace(",", "", $pubDate); //Get rid of commas
  $pubDate = str_replace(":", "", $pubDate); //Finally, get rid of colons
  $pubDay = substr($pubDate, 3, 2); //Should get 13
  $pubMonth = substr($pubDate, 5, 3); //Should get Mar, we'll format it later
  $pubYear = substr($pubDate, 8, 4); //Returns 2009
  $pubHour = substr($pubDate, 12, 2); //Returns 11
  $pubMin = substr($pubDate, 14, 2); //Returns 40
  $pubSec = substr($pubDate, 16, 2); //Returns 02
  $numMonth = array_search($pubMonth, $months); //Find the key or numerical value for the month.
  return mktime($pubHour, $pubMin, $pubSec, $numMonth, $pubDay, $pubYear);
  //return mktime($pubHour, $pubMin, $pubSec, $numMonth, $pubDay, $pubYear);
}

$time = "Fri, 13 Mar 2009 11:40:02 +0000"; //pubDate
echo date("M-d-Y", pub2stamp($time)); //Make real date.
?>

 

I tested it, it should work. It did on my server atleast.

Link to comment
https://forums.phpfreaks.com/topic/149228-rss-pubdate-to-time/#findComment-784483
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.