calculate days-ago from a date string

I’ve got rss feed items in this format:

“Fri, 22 Jul 2011 22:01:10 +0000”

Now I need to check if items in a feed are older than a certain number of days.

Does anyone have a good suggestion how might I convert a date in that format to Epoch time or something useful to do calculations on?

I wrote a little function to convert that date format to:

Friday, July 22, 2011

and I have a function that will tell me what the date was 7 days earlier than today:

Monday, July 18, 2011

What I need to know is how to tell that

Fri, 08 Apr 2011 19:00:25 +0000

is outside the range of 7 days and so exclude it from the list.

Alternatively, any wordpress guru’s who know what, if any http parameters let you limit an rss feed to the last 7 days might be an even better solution…

Maybe I just need some sleep, but I’m blanking on this one.

  • Joel

I can’t think of any easy way to do that other than splitting the string and parsing it directly. Something like…

dateString = "Fri, 22 Jul 2011 22:01:10 +0000"
dateParts = dateString.Tokenize(" ")

day = dateParts[1]
If day.Len() = 1 Then
    day = "0" + day
End If

month = dateParts[2]
If month = "Jan" Then
    month = "01"
Else If month = "Feb" Then
    month = "02"
Else If...
...
End If

year = dateParts[3]
time = dateParts[4]
iso8601 = year + "-" + month + "-" + day + " " + time + ".000"

parsedDate = CreateObject("roDateTime")
parsedDate.FromISO8601String(iso8601)

Awesome, thanks! Here’s what I wrote based on that:


function datediff(date as string) as boolean
	d1=createobject("roDatetime") 'today
	d2=createobject("rodatetime") 'this will be date in past
	d1.Mark() 'set today
	d2.FromISO8601String(isodate(date))
	now=d1.asseconds()
	past=d2.asseconds()
	if (now-past) / 86400 > 7 then 
	?(now-past)/86400
	return false
	else
	return true
	end if 
end function

function isomonsub(input as string) as string
	months={jan:"01",feb:"02",mar:"03",apr:"04",may:"05",jun:"06",jul:"07",aug:"08",sep:"09",oct:"10",nov:"11",dec:"12"}
	return months[input]
end function

function isodaysub(day as string) as string
	if len(day)=1 then day="0"+day
	return day	
end function

function isodate(date as string) as string
	phase1=date
	phase2=phase1.tokenize(" ")
	day=isodaysub(phase2[1])
	month=isomonsub(phase2[2])
	year=phase2[3]
	time=phase2[4]
	return year + "-" + month + "-" + day + " " + time + ".000"
end function