开发者

Coldfusion DateDiff using time format over 24 hours

开发者 https://www.devze.com 2023-02-03 17:27 出处:网络
I am currently tracking the amount of time people are on our site. Each user has an amount of time set for them, whether it\'s 00:10:00 (10 mintues) or 100:00:00 (100 hours).

I am currently tracking the amount of time people are on our site.

Each user has an amount of time set for them, whether it's 00:10:00 (10 mintues) or 100:00:00 (100 hours).

开发者_C百科

The issue comes when I am using this line of code:

<cfset seconds_left = datediff("s",variables.time_used,form.site_time)>

If either one of those variables is over 24 hours (24:00:00) it will throw an error:

40:00:00 is an invalid date or time string.

Any suggestions on how to fix this issue would be great, thank you!


Option 1: track valid date-time values. On login (or first visit) set <cfset session.start_time = now()>. To determine the duration the calculation is as such:

<cfset duration = dateDiff("s", session.start_time, now()))>

Option 2: turn time values you're working with into seconds and do simple subtraction.

<cffunction name="ts2secs" output="false" access="public" returntype="numeric" hint="Take a time string and return number of seconds">
    <cfargument name="ts" type="string" required="true" hint="formated as h:m:s"/>
    <cfset var local = structNew()/>
    <cfset var pieces = listToArray(arguments.ts, ":")>
    <cfset var hours = pieces[1]>
    <cfset var minutes = pieces[2]>
    <cfset var seconds = pieces[3]>
    <cfreturn (hours * 60 * 60) + (minutes * 60) + seconds>
</cffunction>

<cfset duration = ts2secs(variables.time_used) - ts2secs(form.site_time)>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号