CFLib.org – Common Function Library Project

stringHours(mins)

Last updated November 26, 2010

author

Ryan Jeffords

Version: 1 | Requires: CF6 | Library: DateLib

Description:
This simple UDF will accept a number of minutes and return a string containing the format: "N hours N mins". This format can be slightly more optimal then just displaying "140 mins".

Return Values:
Returns a string.

Example:

<cfoutput>
<ul>
    <li>#stringHours("123")#</li>
    <li>#stringHours("568")#</li>
    <li>#stringHours("56")#</li>
    <li>#stringHours("3")#</li>
    <li>#stringHours("1357")#</li>
    <li>#stringHours("99")#</li>
    <li>#stringHours("45.26")#</li>
    <li>#stringHours("9871")#</li>
    <li>#stringHours("54")#</li>
    <li>#stringHours("10")#</li>
</ul>
</cfoutput>

Parameters:

Name Description Required
mins Numerical number of minutes. Yes

Full UDF Source:

<!---
 Formats a number of minutes into into a nicely formatted string.
 
 @param mins      Numerical number of minutes. (Required)
 @return Returns a string. 
 @author Ryan Jeffords (rjeffords@me.com) 
 @version 1, November 26, 2010 
--->
<cffunction name="stringHours" returntype="string" output="false">
    <cfargument name="mins" type="numeric" required="true" />
    
    <cfset var retVal = "" />
    <cfset var rawHours = mins/60 />
    
    <!--- Calculate the hours --->
    <cfset var hours = int(rawhours) />    
    <cfif hours is "1">
        <cfset hours = hours & " hour " />
    <cfelse>
        <cfset hours = hours & " hours " />
    </cfif>
    
    <!--- Calculate the minutes --->
    <cfset mins = round((rawHours - int(rawHours)) * 60) />    
    <cfif mins is "1">
        <cfset mins = mins & " min " />
    <cfelse>
        <cfset mins = mins & " mins " />    
    </cfif>
    
    <!--- Add the hours --->
    <cfif left(hours, 1) IS NOT 0>
        <cfset retVal = hours />
    </cfif>
    
    <!--- Now bring them all together --->
    <cfif left(mins, 1) IS NOT 0>
        <cfset retVal = retVal & mins />
    </cfif>
    
    <cfreturn retVal />
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson