2 Replies - 1689 Views - Last Post: 24 March 2009 - 10:59 AM Rate Topic: -----

#1 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

dayinmonth function not working

Posted 24 March 2009 - 10:10 AM

I built a calendar but the daysinmonth() function doesn't work. It thinks everymonth has 31 days. I tried putting in different months but they all come up as 31 days.

<!---Set the width and height parameters for the table--->
<cfparam name="width" default="98%">
<cfparam name="height" default="100%">
<!---Set the parameter to tell the Calendar to use the full day names--->
<cfparam name="daynames" default="full">
<!---Set the parameter for the current Date--->
<cfparam name="currentyear" default="#year(now())#">
<cfparam name="currentmonth" default="#month(now())#">
<cfparam name="currentday" default="#day(now())#">

<!--- If this page was reached through a link on the calendar create variables for it--->
<cfif isDefined('datelast')>
	<cfif datelast eq createdate(currentyear,currentmonth,1)>
	<cfelse>
		<cfset currentyear = year(url.datelast)>
		<cfset currentmonth = month(url.datelast)>
		<cfset currentday = day(url.datelast)>
	</cfif>
<cfelseif isDefined('datenext')>
	<cfif datenext eq createdate(currentyear,currentmonth,1)>
	<cfelse>
		<cfset currentyear = year(url.datenext)>
		<cfset currentmonth = month(url.datenext)>
		<cfset currentday = day(url.datenext)>
	</cfif>
</cfif>


<cfset firstofmonth = createdate(currentyear,currentmonth,1)>
<cfset dow = dayofweek(firstofmonth)>
<cfset pad = dow - 1>


<cfif currentmonth eq 1>
	<cfset lastyear = currentyear - 1>
	<cfset nextyear = currentyear>
	<cfset lastmonth = 12>
	<cfset nextmonth = 2>
<cfelseif currentmonth eq 12>
	<cfset nextyear = currentyear + 1>
	<cfset lastyear = currentyear>
	<cfset nextmonth = 1>
	<cfset lastmonth = 11>
<cfelse>
	<cfset lastyear = currentyear>
	<cfset nextyear = currentyear>
	<cfset nextmonth = currentmonth + 1>
	<cfset lastmonth = currentmonth - 1>
</cfif>	


<!---
Current Year: <cfdump var="#currentyear#"><br />
Current Month: <cfdump var="#currentmonth#"><br />
Current Day: <cfdump var="#currentday#"><br />
Days in month: <cfdump var="#daysinmonth(currentmonth)#">
Month Start On: <cfdump var="#firstofmonth#"><br />
Next Months Year: <cfdump var="#nextyear#"><br />
Last Months Year: <cfdump var="#lastyear#"><br />
Next Month: <cfdump var="#nextmonth#"><br />
Last Month: <cfdump var="#lastmonth#"><br />
--->

<!---Set the first day of the month, the day of the weekd of that day and the amount of padding necessary into variables--->
<cfset firstOfTheMonth = createDate(currentyear,currentmonth,1)>
<cfset dow = dayofWeek(firstOfTheMonth)>
<cfset pad = dow - 1>

<!--- Create the table header with the names of each day--->
<cfoutput>
<table border="1" width="#width#" height="#height#">
	<tr>
		<th colspan="7">
			<cfoutput>
				<cfif daynames eq 'full'><a href="monthview.cfm?datelast=#createdate(lastyear,lastmonth,1)#" style="text-decoration:none; ">&lt;&lt;</a></cfif>
				&nbsp;&nbsp;&nbsp; #monthasstring(currentmonth)# #currentyear# &nbsp;&nbsp;&nbsp;
				<cfif daynames eq 'full'><a href="monthview.cfm?datenext=#createdate(nextyear,nextmonth,1)#" style="text-decoration:none; ">&gt;&gt;</a></cfif>
			</cfoutput>
		</th>
	</tr>
	<tr>
	 <cfloop index="x" from="1" to="7">
			<th width="14%">
				<cfif daynames eq 'full'>
					#dayOfWeekAsString(x)#
				<cfelseif daynames eq 'short'>
					
				</cfif>
			</th>
	 </cfloop>
	</tr>
</cfoutput>
	<!---Start the row--->
	<tr>
		<!---If the pad variable is greater than 0 then create a column that is that wide.--->
		<cfif pad gt 0>
			 <cfoutput><td colspan="#pad#">&nbsp;</td></cfoutput>
		</cfif>
		<!---Figure out how many days are in the month--->
		<cfset days = daysInMonth(currentmonth)>
		<!--- Set the counter to include the pad variable--->
		<cfset counter = pad + 1>
		<!---Begin the loop through the days in the month--->
		<cfloop index="x" from="1" to="#days#">
			<!---If we are on the current day then highlight it in yellow--->
			 
			 <cfif x is currentday>
					<cfif daynames eq 'full'>
						<td bgcolor="yellow" valign="top">
					<cfelse>
						<td valign="middle" align="center">
					</cfif>
			 <cfelse>
					<td valign="top">
			 </cfif>
			 <!---Actually output the day #--->
			 <cfoutput>
			 #x#</td>
			 </cfoutput>
			 <!---Add one to the counter--->
			 <cfset counter = counter + 1>
			 <!---If the counter hites 8 than the end of the week has been reached the row must end--->
			 <cfif counter is 8>
				 </tr>
					<!---If it isn't the end of the month then set the counter back to 1--->
					<cfif x lt days>
						 <cfset counter = 1>
						 <cfoutput>
						 <tr>
						 </cfoutput>
					</cfif>
			 </cfif>
		</cfloop>
		<!---If the counter is not 8 but it is the end of the month then create a new column that is enough cloumns wide to finish the row.--->
		<cfif counter is not 8>
			 <cfset endPad = 8 - counter>
			 <td colspan="#endPad#">&nbsp;</td></tr>
		</cfif>
</table>



Has anyone else ever had this problem? If so how did you fix it.

Is This A Good Question/Topic? 0
  • +

Replies To: dayinmonth function not working

#2 sansclue  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: dayinmonth function not working

Posted 24 March 2009 - 10:46 AM

View Postxheartonfire43x, on 24 Mar, 2009 - 09:10 AM, said:

<cfset days = daysInMonth(currentmonth)>


Strictly speaking daysInMonth() does accept numeric dates, but usually you pass in a full date like "2009-03-24". A month number like (1,2,3, ... 12) gets treated as a numeric date and evaluates to something like December 1899 or January 1900. Those months do have 31 days.
Was This Post Helpful? 0
  • +
  • -

#3 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: dayinmonth function not working

Posted 24 March 2009 - 10:59 AM

View Postsansclue, on 24 Mar, 2009 - 09:46 AM, said:

View Postxheartonfire43x, on 24 Mar, 2009 - 09:10 AM, said:

<cfset days = daysInMonth(currentmonth)>


Strictly speaking daysInMonth() does accept numeric dates, but usually you pass in a full date like "2009-03-24". A month number like (1,2,3, ... 12) gets treated as a numeric date and evaluates to something like December 1899 or January 1900. Those months do have 31 days.



I added a createdate() function and it works perfectly now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1