I am not sure what to call this type of date/time formatting, but I had a need to format time in the “Posted x minutes ago” format using ColdFusion. I couldn’t find much data on the Internet on how this should be done, but I was able to convert a PHP function that was posted to ColdFusion.
<cffunction name="ago" access="public" output="true">
<cfargument name="sDate" type="date" required="true">
<cfset dateparts = ArrayNew(1)>
<cfset dateparts[1] = "yyyy">
<cfset dateparts[2] = "m">
<cfset dateparts[3] = "d">
<cfset dateparts[4] = "h">
<cfset dateparts[5] = "n">
<cfset datepartNames = ArrayNew(1)>
<cfset datepartNames[1] = "year">
<cfset datepartNames[2] = "month">
<cfset datepartNames[3] = "day">
<cfset datepartNames[4] = "hour">
<cfset datepartNames[5] = "minute">
<cfloop from="1" to="5" index="i">
<cfset diff = DateDiff(variables.dateparts[#i#], sDate, now() )>
<cfif diff gt 1>
<cfreturn "#diff# #variables.datepartNames[i]# ago">
</cfif>
<cfif diff eq 1>
<cfreturn "#diff# #variables.datepartNames[i]# ago">
</cfif>
</cfloop>
<cfreturn "Just Now">
</cffunction>