« Back to the main page

Language: Coldfusion, Submitted: 1/04/12, Country: United States - Raw | Download
<cffunction name='RelativeTime' hint='Displays a timestamp as "2 days ago" for example - MB 120104'>
    <cfargument name='Date' required='yes'>
    <cfargument name='Format' required='no' default='m/d/y'>
    <cfargument name='OnHover' required='no' default='true'>
    <cfscript>
 
        minut = 60;
        hou = 60 * minut;
        da = 24 * hou;
        wee = 7 * da;
        mont = 4.35 * wee;
 
        diff = DateDiff("s", Date, now());
 
        mins = round(diff / minut);
        hours = round(diff / hou);
        days = round(diff / da);
        weeks = round(diff / wee);
        months = round(diff / mont);
 
        timestamp = TimeFormat(date,'h:mm tt');
        monthstamp = dateformat(date,'MMM d');
        yearstamp = dateformat(date,format);
        longstamp = dateformat(date,'DDDD, MMMM d, YYYY') & " " & TimeFormat(date,"h:mm tt");
        endstamp = '';
 
        if (diff <= minut) {
            if (diff < 0) endstamp = "sometime in the future";
            endstamp = timestamp & " (" & diff & " second" & (diff!=1?"s":"") & " ago)";
        } else if (diff <= hou) {
            endstamp = timestamp & " (" & mins & " minute" & (mins!=1?"s":"") & " ago)";
        } else if (diff <= da) {
            endstamp = timestamp & " (" & hours & " hour" & (hours!=1?"s":"") & " ago)";
        } else if (diff < 2*wee) {
            endstamp = monthstamp & " (" & days & " day" & (days!=1?"s":"") & " ago)";
        } else if (diff < mont) {
            endstamp = monthstamp;
        } else {
            endstamp = yearstamp;
        }
 
        if (OnHover)
            return '<span title="' & longstamp & '">' & endstamp & '</span>';
        else
            return endstamp;
    </cfscript>
</cffunction>