In this example, we will check the current time and then display an appropriate greeting based on the time of day. -
In your text editor, type the code shown in Listing 4.1, or you can open the Greeting.cfm file from the CompletedFiles\Examples\Step04 folder. Listing 4.1 Greeting.cfm <!--- File: Greeting.cfm Description: Demonstrate the use of <CFIF> Author: Created: ---> <HTML> <HEAD> <TITLE>Daily Greeting</TITLE> </HEAD> <BODY> <!--- Check if it is morning or afternoon and customize greeting Use the Now() function to get the current Date and time Use the Hour() function to isolate just the hour The Hour() function returns a value of 0 - 23 ---> <H2> <CFIF Hour(Now()) LT 12> Good morning, <CFELSE> Good afternoon, </CFIF> welcome to my site. </H2> <P>I am glad you could make it.</P> </BODY> </HTML> -
Save this file as Greeting.cfm in the Examples\Step04 folder. -
Browse to the file. You should see something similar to Figure 4.1. Figure 4.1. The Greeting.cfm display. -
Change your system time from a.m. to p.m. or vice versa. -
Refresh the page. The greeting should change.
|