views:

99

answers:

4

For ColdFusion I would like to use a cfswitch statement to have different code for each season of the year. For example to display <p>Winter</p> for winter and <p>Summer</p> for summer, etc. What is the best approach to achieve this?

A: 

Just define when your seasons start and check that in your application when it starts.

I think putting it in onSessionStart makes sense.

Faisal Abid
A: 

Technically, the dates that seasons officially begin and end vary from year to year. If you need to be exact, you could use an external properties file (for easy yearly maintenance) that contains the date ranges for each season. If you don't need to be that precise, you could just hard code the date ranges using approximate dates, e.g. Summer starts on 6/21. When the page is loaded or when the session starts, get the current date with Now(), and compare it to the date ranges to find out which season it corresponds to.

Segphault
A: 

Create a season.cfc component with a getSeason() method which always returns the current season. You can write getters and setters for begin and enddate of various seasons. Or create a function that retrieves season dates from xml or profile string.

<cfscript>
    function getSeason(){
        var currentdatetime = DateFormat(now(), "MMDD");
        var season = "";
        // Spring
        var springbegin = "0321";
        var springend = "0620";
        // Sommer
        var summerbegin = "0621";
        var summerend = "0922";
        // Fall
        var fallbegin = "0923";
        var fallend = "1220";
        // Winter begin of year
        var winter1begin = "0101";
        var winter1end = "0320";
        // Winter end of year
        var winter2begin = "1221";
        var winter2end = "1231";
        if ((currentdatetime gte springbegin) and (currentdatetime lte springend)){
            season = "spring";
        } else if ((currentdatetime gte summerbegin) and (currentdatetime lte summerend)){
            season = "summer";
        } else if ((currentdatetime gte fallbegin) and (currentdatetime lte fallend)){
            season = "fall";
        } if ((currentdatetime gte winter1begin) and (currentdatetime lte winter1end)){
            season = "winter";
        } if ((currentdatetime gte winter2begin) and (currentdatetime lte winter2end)){
            season = "winter";
        } 
        return season;
    }
</cfscript>

<cfset currentseason = getSeason()>

Define the text.

<cfset seasonText = structNew()>    
<cfset seasonText["winter"] = "Winter">
<cfset seasonText["spring"] = "Spring">
<cfset seasonText["summer"] = "Summer">
<cfset seasonText["fall"] = "Fall">

This is what you have in your View

<p>#seasonText[currentseason]#</p>

You can also use this technique to show different Images, depending on the season.

Andreas Schuldhaus
A: 

Using the code provided I was able to create the cfswitch statement that I wanted. Here is an example:

<cfswitch expression="#getSeason()#">
  <cfcase delimiters="," value="Winter">
    <p>Winter Edition</p>
    <p><img src="/images/winterPhoto.jpg" alt="Winter Image" /></p>
  </cfcase>
  <cfcase delimiters="," value="Spring">
    <p>Spring Edition</p>
    <p><img src="/images/springPhoto.jpg" alt="Spring Image" /></p>
  </cfcase>
  <cfcase delimiters="," value="Summer">
    <p>Summer Edition</p>
    <p><img src="/images/summerPhoto.jpg" alt="Summer Image" /></p>
  </cfcase>
  <cfcase delimiters="," value="Fall">
    <p>Fall Edition</p>
    <p><img src="/images/fallPhoto.jpg" alt="Fall Image" /></p>
  </cfcase>
  <cfdefaultcase>
    <p><img src="/images/neutralPhoto.jpg" alt="Neutral Image" /></p>
  </cfdefaultcase>
</cfswitch>

This enables me to have any code I want for each season of the year. I don't find having the exact date necessary. It's a lot of extra work and maintenance for only being off a few days. Thanks a lot to everyone for their help.

geckomist
I put you in the right direction. You used the code i provided, but didn't accept it as an answer. Instead, you accepted your own summary. Therefore I vote this answer down.
Andreas Schuldhaus
Well I'm new to this and didn't even know you could specify an answer. I apologize for this and I'll fix it.
geckomist