10th January 2004, mid-afternoon | Comments (16)
A while back, Natalie Downe published a very helpful bit of PHP code called time_since, which represents dates as the number of years, months, days, hours and minutes that have passed since that date. For example: “2 days, 6 hours and 9 minutes ago” rather than the more traditional “Thurs 8th January 2003 at 07:00”.
I don’t use this function on my site’s main pages (because here the full dates play an important role in site structure and linking), however I do use it in my blogroll (coming soon) where a relative timestamp is more appropriate than an absolute one.
What are the benefits of time_since? Well, dates represented in the “Thurs 8th January 2003 at 07:00” format demand a certain amount of mental gymnastics:
Compare that to, “Oh, it was posted a couple of days ago, that’s old” and the difference in usability is obvious.
To cover all my bases however, I code my blogroll’s datetime statements like this:
<li>
sidesh0w.com
<br />
<span title="Thurs 8th January 2003, 0700 +0000 UTC">2 days, 6 hours and 9 minutes ago<span>
</li>
The reason I bring all this up is that I’m in the midst of adapting an ASP-coded forum and I wanted to swap all the post dates over to this friendly new format.
Enter Cam McVey, a wonderful chap who is embarrassingly helpful when I get stuck with the finer points of ASP and VB. Here is Cam’s five minute conversion of time_since into ASP/VB:
<%
' Very kindly knocked up by Cam McVey
' http://mcvey.org/
' Based on an original script by Natalie Downe
' http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
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 = "!!!ERROR!!!"
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()
' set Locale to US datetime format (for example)
' http://www.drdev.net/LocaleIDs.asp
varLocale = SetLocale(1033)
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 & "1 " & sName
Else
sPrint = sPrint & iCount & " " & sName & "s"
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 & ", 1 " & sName2
Else
sPrint = sPrint & ", " & iCount2 & " " & sName2 & "s"
End If
End If
End If
Set dictChunks = Nothing
Erase aKeys
TimeSince = sPrint
End Function
%>
TimeSince() is written to work on dates (not UNIX timestamps like its PHP counterpart). The datetime format of the string that you pass to TimeSince() has to match that of the datetime strings generated by your server. If it doesn’t (so say you pass a US-format date to TimeSince() running on a UK server) then you’ll get problems.
Luckily you can tell your server what country-format you’d like it to generate datetime strings in. For this we use LocaleID.
So if I have a UK-based server, and I pass TimeSince() a US-format date, I could use LocaleID(1033) to force the server to generate a US-format date when it compares ‘then’ to ‘now’.
Does that make sense? I hope so.
Location | LocaleID | Format 0 | Format 1 | Format 2 | Format 3 |
---|---|---|---|---|---|
UK | 2057 | 10/01/2004 14:48:49 | 10 January 2004 | 10/01/2004 | 14:48:49 |
USA | 1033 | 1/10/2004 2:48:49 PM | Saturday, January 10, 2004 | 1/10/2004 | 2:48:49 PM |
(The relevant lines to deal with this are marked up in the ASP script.)
And that’s that, I hope someone finds it useful. I’m wondering, though, what you guys and gals think of this ‘time since’ presentational method? Do you like it? Is it helpful? Or is it a pain? Be honest.
Jump up to the start of the post ↑
A collection of miscellaneous links that don't merit a main blog posting, but which are interesting none-the-less.
Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we.— George W Bush (9)
Stuff from the intersection of design, culture and technology.(3)
A selection of blogs I read on a regular basis.