CFLib.org – Common Function Library Project

getTimeInterval(date)

Last updated July 27, 2012

author

Simon Bingham

Version: 1 | Requires: CF9 | Library: DateLib

Description:
Returns the interval since a date in a Twitter like format (e.g. 4 seconds ago, 5 minutes ago, 6 hours ago, yesterday, etc).

Return Values:
Returns a string

Example:

<cfoutput>#getTimeInterval(createDateTime(2012, 2, 22, 12, 30, 0))#</cfoutput>

Parameters:

Name Description Required
date Date to format Yes

Full UDF Source:

/**
 * Returns the interval since a date in a Twitter like format (e.g. 5 minutes ago)
 * version 0.9 by Simon Bingham
 * version 1.0 by Adam Cameron - tweaked to correct pluralisation when the interval value was 1.
 * 
 * @param date 	 Date to format (Required)
 * @return Returns a string 
 * @author Simon Bingham (me@simonbingham.me.uk) 
 * @version 1, July 27, 2012 
 */
function getTimeInterval(date){
	var interval	= "";
	var offset		= 0;
	var result		= 0;
	if (isDate(arguments.date)){
		var formattedDate = dateFormat(arguments.date, "dddd dd mmmm yyyy") & " at " & timeFormat(arguments.date, "HH:MM");
		
		if (dateDiff("s", arguments.date, now()) < 60){
			// less than 1 minute show interval in seconds
			offset	= dateDiff("s", arguments.date, now());
			interval= offset == 1 ? "second":"seconds";
			result	= "#offset# #interval# ago";
		
		}else if (dateDiff("n", arguments.date, now()) < 60){
			// less than 1 hour show interval in minutes
			offset	= dateDiff("n", arguments.date, now());
			interval= offset == 1 ? "minute":"minutes";
			result	= "#offset# #interval# ago";
		
		}else if (dateDiff("h", arguments.date, now()) < 24){
			// less than 24 hours display interval in hours
			offset	= dateDiff("h", arguments.date, now());
			interval= offset == 1 ? "hour":"hours";
			result	= "#offset# #interval# ago";
		
		}else if (dateDiff("d", arguments.date, now()) < 2){
			// less than 2 days display yesterday
			result	= "yesterday";
		}else if (dateDiff("d", arguments.date, now()) < 7){
			// less than 7 days display day
			result	= dayOfWeekAsString( dayOfWeek( arguments.date ));
		}else{
			// otherwise display date
			result	= formattedDate;
		}
		
		interval = "<abbr title='" & formattedDate & "'>" & result & "</abbr>";
	}
	return interval;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson