Functions returning TRUE or FALSE
Morning chaps,
Wrote a function that converts a date in the format "01 Jan 2010" to a Unix timestamp. It also returns FALSE if it cannot convert the date it is being passed, which is great because I can use:
<cfif convertDateToUTS(FORM.date)>
<cfset #date# = #convertDateToUTS(FORM.date)# />
<p>Your date: <cfoutput>#date#</cfoutput></p>
<cfelse>
<p>Could not convert date.</p>
</cfif>
Here is the function:
<cffunction name="convertDateToUTS" output="no"> <cfargument name="date" required="Yes" /> <cfargument name="time" default="00:00"/> <cftry> <cfset #date# = trim(left(trim(date), 11)) /> <cfset #time# = trim(left(trim(time), 5)) /> <cfset #dateArr# = #listToArray(date, " ")# /> <cfset #dateArr[2]# = #replace(dateArr[2], "Jan", 1)# /> <cfset #dateArr[2]# = #replace(dateArr[2], "Feb", 2)# /> ...etc... <cfset #dateArr[2]# = #replace(dateArr[2], "Dec", 12)# /> <cfset #dateTime# = ArrayToList(dateArr, " ")&" "&time /> <cfset #dateTimeBits# = listToArray(dateTime, " :") /> <cfset #ODBCdateTime# = CreateODBCDateTime(CreateDateTime(dateTimeBits[3], dateTimeBits[2], dateTimeBits[1], dateTimeBits[4], dateTimeBits[5], 0)) /> <cfset #unixDateTime# = #DateDiff("s", CreateDate(1970,1,1), ODBCdateTime)# /> <cfreturn #unixDateTime# /> <cfcatch> <cfreturn FALSE /> </cfcatch> </cftry> </cffunction>
However, now I want to write a similar one which converts a timestamp to a date in that format:
<cffunction name="convertUTStoDate" output="no"> <cfargument name="uts" required="Yes" /> <cftry> <cfset #date# = #DateFormat(DateAdd("s",uts,"1970/01/01 00:00:00"),"DD MMM YYYY")# /> <cfset #time# = #TimeFormat(DateAdd("s",uts,"1970/01/01 00:00:00"), "HH:mm")# /> <cfreturn #date#&" "&#time# /> <cfcatch> <cfreturn FALSE /> </cfcatch> </cftry> </cffunction>
This does the conversion correctly but it it won't let me use it as the condition in a CFIF like the first function does eg <cfif convertUTStoDate(uts)> but, it does work exactly as I want if I do this: <cfif convertUTStoDate(uts) NEQ FALSE>
What am I doing wrong? I really want it to behave like the first function when used in a condition. Massive thanks to anyone who can help!
T
