CFTIME: How to Make Content Appear and Disappear

Content on the web often has a shelf life. You may have information that you want up after a certain point in time or you may want it to be gone after a certain time.You may want to have it appear and disappear without having to be editing files or changing out input in some back end system. You will find that you can build a simple system for publishing and removing info with ColdFusion's CFIF tag and CreateDateTime() and Now() functions.

CFIF is one of the most basic and commonly used tags in ColdFusion. We will use it to hide or display content based on comparing a set date to the time that the page is parsed for the visitor.

The CreateDateTime() function takes date and time parameters and creates a ColdFusion date/time object. The format is CreateDateTime(year,month, day, hour, minute, second). An example would be CreateDateTime (2004, 7, 9,19,59, 0) for July 9, 2004 at 7:59pm.

The Now() function takes no parameters and produces a ColdFusion date/time object for the current date and time including seconds.

Example Code

The components are simple so let's put it all together. Our first example is content that displays until a future point in time. In this case if the date/time is less than the target date your content will display. This would be usefull if yo had a contest running and wanted to remove the content from your site when the contest was over.

<cfif #now()# lte #CreateDateTime (2004, 7, 9,19,59, 0)#>
This content is displayed until July 7, 2004 at 7:59pm.

</cfif>

Our next example is content that displays after a future point in time. In this case if the date/time is greater than the target date your content will display. This would be handy if you had an announcement coming up so you could announce it on your web site without having to sit at your computer and trigger the new content.

<cfif #now()# gte #CreateDateTime (2004,7,5,10,30, 0)# >
This content is displayed after July 5, 2004 at 10:30am.
</cfif>

Our last example is content that displays after a future point in time and ends at a later date. In this case if the date/time is greater than the target date and it was smaller than a second target date, your content will display. This would be handy if you had an announcement coming up so you could announce it on your web site without having to sit at your computer and trigger the new content and it would also remove the announcement at the appropriate time.

<cfif #now()# lte #CreateDateTime (2004, 7, 9,19,59, 0)#
AND #now()# gte #CreateDateTime (2004,5, 2,10,30, 0)# >
This content is displayed after May 5, 2004 at 10:30am and disappears after July 7, 2004 at 7:59pm .
</cfif>

Summary

ColdFusion makes dealing with times and dates easy with built in functions like CreateDateTime() and Now(). Developers won't need to be on call for special announcements or the end of a campaign. Test out some examples of your own making and then don't let the clock and your computer dictate when you are making content appear and disappear.

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.