Skip to page content or Skip to Accesskey List.

Work

Main Page Content

On Time Every Time Timing Material In Cf

Rated 3.78 (Ratings: 2)

Want more?

  • More articles in Code
 
Picture of gothwalk

Drew Shiel

Member info

User since: 05 Mar 2002

Articles written: 1

Timed-in content is becoming more and more useful on many sites, as promotions are carefully engineered to coincide with holidays, advertising broadcasts, sporting events and peak internet usage times. You can use this code for something as simple as changing your graphics over holidays, all the way up to arranging a half-price happy hour to go off between six and seven in the evening, while you're commuting home.

The really important line of code here is:

<cfif (DateDiff("n",now(),"December 18 2004 12:00") lte 0) and (DateDiff("n",now(),"December 25 2004 23:59") gte 0)>

Essentially, all we're doing here is check that the difference between a start time - noon on the 18th of December - and the time now is less than or equal to zero, and that the difference between the time now and the end time, just-before-midnight on the 25th, is greater than zero. The "n" sets the date check to be sensitive to the minute. "h" would set it to be sensitive to the hour instead - this isn't usually as useful, though. This period can be as long or as short as you like.

So, if you're changing your logo for the run up to Christmas, like Google does, you'd do:

<cfif (DateDiff("n",now(),"December 18 2004 12:00") lte 0) and (DateDiff("n",now(),"December 25 2004 23:59") gte 0)>

   <img src="images/xmaslogo.gif">

<cfelse>

   <img src="images/logo.gif">

</cfif>

You can also do a series of images, for a countdown, say, all timed in day by day:

<cfif (DateDiff("n",now(),"March 14 2004 00:00") lte 0) and (DateDiff("n",now(),"March 14 2004 23:59") gte 0)>

   <img src="images/3-days-to-go.gif">

<cfelseif (DateDiff("n",now(),"March 15 2004 00:00") lte 0) and (DateDiff("n",now(),"March 15 2004 23:59") gte 0)>

   <img src="images/2-days-to-go.gif">

<cfelseif (DateDiff("n",now(),"March 16 2004 00:00") lte 0) and (DateDiff("n",now(),"March 16 2004 23:59") gte 0)>

   <img src="images/1-day-to-go.gif">

<cfelseif (DateDiff("n",now(),"March 17 2004 00:00") lte 0) and (DateDiff("n",now(),"March 17 2004 23:59") gte 0)>

   <img src="images/happy-paddys-day.gif">

</cfif>

However, that's a big chunk of code if you have multiple images or lines of text timing in, so it may well be more useful and clearer to this code at the top of the page:

<cfif (DateDiff("n",now(),"September 3 2004 00:00") lte 0) and (DateDiff("n",now(),"September 3 2004 23:59") gte 0)>

   <cfset timein ="on">

</cfif>

And then surround your time-dependent content with:

<cfif timein is "on">

   Happy Birthday!

</cfif>

I've been using this code in production for some months now, and I can say it's saved me hours of overtime and awkwardness with timed material. Hopefully, it will do the same for you.

Drew lives in Dublin, Ireland, with his wife and cat. You can read all his adventures on a day-to-day basis on his livejournal - the new opium of the masses. He works for Sports Interaction by day, and spends most of his spare time in other worlds, doing other things. A lot of this is detailed on dukestreet.org, his science fiction & fantasy site.

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.org Evolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.