Most of my clients have asked in the past to have a date on the article so that their clients can see when the article was updated. Now since the web is international it doesn’t make sense to say this:

Article updated : 03/05/2009 11:56:23 am

As Americans would think that this was updated on the 5th March 2009 and Europeans would think that that it was updated on the 3rd May 2009, all most people want to know is how old the article is and here is the cool part. We are going to say this article was updated say 10 minutes ago or 2 months ago.

This is how it is done.

I normally make an include file for asp called time_since.asp and in that file put this:

<%

Function TimeSince(Original)

Dim aChunks, aYear, aMonth, aWeek, aDay, aHour, aMinute

Dim dictChunks, aKeys

Dim Today, iSince

Dim i, j

Dim iSeconds, iSeconds2

Dim sName, sName2

Dim iCount, iCount2

Dim sPrint

Dim Locale

If Not IsDate(Original) Then

TimeSince = “”

Exit Function

Else

Original = CDate(Original)

End If

Set dictChunks = Server.CreateObject(“Scripting.Dictionary”)

dictChunks.Add “year”, 60 * 60 * 24 * 365

dictChunks.Add “month”, 60 * 60 * 24 * 30

dictChunks.Add “week”, 60 * 60 * 24 * 7

dictChunks.Add “day”, 60 * 60 * 24

dictChunks.Add “hour”, 60 * 60

dictChunks.Add “minute”, 60

aKeys = dictChunks.Keys()

Original = FormatDateTime(Original, 0)

Today = Now()

iSince = CLng(DateDiff(“s”, Original, Today))

j = dictChunks.Count

For i = 0 To j – 1

iSeconds = CLng(dictChunks.Item(aKeys(i)))

sName = aKeys(i)

iCount = Int(iSince / iSeconds)

If iCount <> 0 Then

Exit For

End If

Next

If iCount = “-1″ Then

sPrint = sPrint & “Today”

Else

sPrint = sPrint & iCount & ” ” & sName & “s” & ” ago”

End If

If i + 1 < j Then

iSeconds2 = dictChunks.Item(aKeys(i + 1))

sName2 = aKeys(i + 1)

iCount2 = Int((iSince – (iSeconds * iCount)) / iSeconds2)

If iCount2 <> 0 Then

If iCount2 = -1 Then

sPrint = sPrint

Else

sPrint = sPrint

End If

End If

End If

Set dictChunks = Nothing

Erase aKeys

TimeSince = sPrint

End Function

%>

And then call the file like this:

Added: <%=timesince((rsNewest.Fields.Item(“company_date_added”).Value))%>

This will come out like this:

Added : 10 Minutes Ago.

Hope that helps you too.

About Paul Ogier

Paul Ogier has written 202 posts.

Paul Ogier is an IT Guru, Website Developer, and generally a nice guy. He loves code and he loves design.