Calculate Next Billing Date
I had a need to figure out the next billing date after today, based on a known start date and billing interval. Here’s the function I came up with to make it happen.
<cffunction name="getNextBilling" access="public" output="false" returntype="date" >
<cfargument
name="StartDate"
type="date"
required="true"
hint="The date the billing started"
/>
<cfargument
name="BillingInterval"
type="numeric"
required="true"
hint="The number of units for the billing. For example,
if something is billed every 90 days, this value will be 90"
/>
<cfargument
name="IntervalUnit"
type="string"
default="d"
required="false"
hint="The date part for the billing interval. This is the
CF datepart, such as 'd', 'm', 'yyyy', etc. The default is 'd'."
/>
<cfset var TimeFromStart = DateDiff(Arguments.IntervalUnit, StartDate, Now()) />
<cfreturn DateAdd(Arguments.IntervalUnit, TimeFromStart + Arguments.BillingInterval - (TimeFromStart MOD Arguments.BillingInterval), Arguments.Startdate) />
</cffunction>
Let me know if you spot a problem with the solution, or have suggestions for improvement.
2 Comments
Zooby wrote on 03/04/11 3:12 PM
Hey Dan this is Zooby from the stackoverflow forum. I just wanted to know if I could email you my code that I'm working on and maybe it might make better sense to you. It wont let me post it on the forum because its too much text.
Chris Blackwell wrote on 12/11/10 5:34 AM
I wrote a little library a while back to handle calculating all sorts of recurring date patterns, simple things like every 90 days from a given date through to more complex patterns like the last thursday of every 3rd month. http://recurrence.riaforge.org/ -Chris