CFLib.org – Common Function Library Project

longTime(seconds)

Last updated January 25, 2012

Version: 2 | Requires: ColdFusion 8 | Library: DateLib

 
Rated 1 time(s). Average Rating: 4.0

Description:
Returns a list like 3 years, 2 months, 12 days, 1 hour, 10 minutes, 45 seconds from a seconds count argument. Correctly adds the (s) to each item month vs. months, etc. Will not include any items that are zero. For exactly 1 year and 20 seconds, would return '1 year, 20 seconds'

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>
Large test: <br/>
111132864 seconds = #longTime(111132864)#<br />
Medium test: <br />
97531 seconds = #longTime(97531)#<br />
Shorter test: <br />
11252 seconds = #longTime(11252)#<br />
</cfoutput>

Parameters:

Name Description Required
seconds Number of seconds. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Returns a list like 3 years, 2 months, 12 days, 1 hour, 10 minutes, 45 seconds from a seconds count argument.
 * Fix by Shad Belcher
 * 
 * @param seconds      Number of seconds. (Required)
 * @return Returns a string. 
 * @author Will Belden (CaptainPalapa@gmail.com) 
 * @version 2, January 25, 2012 
 */

function longTime(seconds) {
    var nSeconds = seconds;
    var nSecondsLeft = seconds;
    var stPieces = {};

    var sComplete = '';
    var sYears = '';
    var sMonths = '';
    var sDays = '';
    var sHours = '';
    var sMinutes = '';
    var sSeconds = '';

    var nYearsSeconds = 31536000;
    var nMonthsSeconds = 2628000; // YearsSeconds divided by 12
    var nDaysSeconds = 86400;
    var nHoursSeconds = 3600;
    var nMinutesSeconds = 60;

    // Calculate YEARS
    stPieces['years'] = fix(nSecondsLeft / nYearsSeconds);
        nSecondsLeft = nSecondsLeft - nYearsSeconds * stPieces['years'];
    stPieces['months']= fix(nSecondsLeft / nMonthsSeconds);
        nSecondsLeft = nSecondsLeft - nMonthsSeconds * stPieces['months'];
    stPieces['days']= fix(nSecondsLeft / nDaysSeconds);
        nSecondsLeft = nSecondsLeft - nDaysSeconds * stPieces['days'];
    stPieces['hours']= fix(nSecondsLeft / nHoursSeconds);
        nSecondsLeft = nSecondsLeft - nHoursSeconds * stPieces['hours'];
    stPieces['minutes']= fix(nSecondsLeft / nMinutesSeconds);
        nSecondsLeft = nSecondsLeft - nMinutesSeconds * stPieces['minutes'];
    stPieces['seconds'] = nSecondsLeft;

    if ( stPieces['seconds'] GT 1){
        sComplete = listPrepend(sComplete, stPieces['seconds'] & ' seconds');
    } else if ( stPieces['seconds'] GT 0 ){
        sComplete = listPrepend(sComplete, stPieces['seconds'] & ' second');
    }
    if ( stPieces['minutes'] GT 1){
        sComplete = listPrepend(sComplete, stPieces['minutes'] & ' minutes');
    } else if ( stPieces['minutes'] GT 0 ){
        sComplete = listPrepend(sComplete, stPieces['minutes'] & ' minute');
    }
    if ( stPieces['hours'] GT 1){
        sComplete = listPrepend(sComplete, stPieces['hours'] & ' hours');
    } else if ( stPieces['hours'] GT 0 ){
        sComplete = listPrepend(sComplete, stPieces['hours'] & ' hour');
    }
    if ( stPieces['days'] GT 1){
        sComplete = listPrepend(sComplete, stPieces['days'] & ' days');
    } else if ( stPieces['days'] GT 0 ){
        sComplete = listPrepend(sComplete, stPieces['days'] & ' day');
    }
    if ( stPieces['months'] GT 1){
        sComplete = listPrepend(sComplete, stPieces['months'] & ' months');
    } else if ( stPieces['months'] GT 0 ){
        sComplete = listPrepend(sComplete, stPieces['months'] & ' month');
    }
    if ( stPieces['years'] GT 1){
        sComplete = listPrepend(sComplete, stPieces['years'] & ' years');
    } else if ( stPieces['years'] GT 0 ){
        sComplete = listPrepend(sComplete, stPieces['years'] & ' year');
    }

    sComplete = replace(sComplete, ',', ', ', 'ALL');
    return sComplete;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

CF Ninja CF Ninja added
calcIRR
13 day(s) ago

Stephen Withington Stephen Withington added
getWeekOfMonth
13 day(s) ago

Adam Cameron Adam Cameron added
createPrimeNumbe...
a while ago

Ray Ford Ray Ford added
timeZoneNow
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson