Jump to content

Month not working, php bug?


imperium2335

Recommended Posts

Hi, this one has me stumped!

 

For some reason February refuses to show up in my calender, that is, month 2 reads as march as well as month 3 (being correct).

 

Here is my code which is fine except for this silly bug:

 

$month = $_GET['month'] ;
$year = $_GET['year'] ;

if(!$month) $month = date('n') ;
if(!$year) $year = date('Y') ;

$days = cal_days_in_month(CAL_GREGORIAN, $month, $year) ;

echo "<div style='width:100%;text-align:center;font-size:1.3em;font-weight:bold;'>" ;
if($month == 1) echo "<a href=\"?month=12&year=" . ($year-1) ;
else echo " <a href=\"?month=" . ($month-1) . "&year=" . $year ; 
echo "\"><img style='border:none;' width='16' src='prev-month.png' /></a>" ;
echo " Classes for " . date("F, Y", strtotime("$year-$month")) . "\n" ;
if($month == 12) echo "<a href=\"?month=1&year=" . ($year+1) ;
else echo " <a href=\"?month=" . ($month+1) . "&year=" . $year ;
echo "\"><img style='border:none;' width='16' src='next-month.png' /></a>\n" ;
echo "</div>\n" ;

for($i=1;$i < 8;$i++) { // Create day headers...
	echo "<div style='float:left;width:110px;margin:20px 5px 0px 5px;text-align:center;background:#1552ff;color:white;border:1px solid #155BFF;'>" . date("l", strtotime("$year-$month-$i")) . "</div>\n" ;
}

for($i=1; $i < $days+1;$i++) { // Create days...

	$result = mysql_query("SELECT *
						   FROM classes
						   WHERE DAY(classDate) = '$i'
						   AND MONTH(classDate) = '$month'
						   AND YEAR(classDate) = '$year'")or die(mysql_error()) ;
		$classCount = mysql_num_rows($result) ;
		while($row = mysql_fetch_assoc($result)) {
		$classes .= $row['title'] . "<br />" ;
	}
	echo "<div style='float:left;width:104px;height:104px;margin:0px 5px 0px 5px; border:1px solid #155BFF;border-top:none;cursor:pointer;padding:3px;' onmouseover=\"this.style.background='#ffbdec';\" onmouseout=\"this.style.background='none';\" title=\"" . str_replace('<br />', "\n\n", $classes) . "\" alt=\"" . str_replace('<br />', "\n\n", $classes) . "\">\n" ;
	echo "<div style='float:left;height:84px;width:104px;'>\n" ;
	echo "<span style='font-weight:bold;'>$i</span><br />\n" ;
	echo "<span style='font-size:0.7em;'>" ;
	echo $classes ;
	echo "</span></div>\n" ;
	echo "<div style='float:left;height:20px;width:104px;text-align:center;'>\n" ;
	echo "<a href='#' style='font-size:0.7em;'>click to book a class</a>\n" ;
	echo "</div>\n" ;
	echo "</div>\n" ;
	$classes = '' ;
}

 

working example here: www.caketopper.co.uk/2011/class-schedule.php

Link to comment
Share on other sites

strtotime uses the current date/time for the fields that you don't explicitly list (it produces a unix timestamp by internally calling the mktime() function that includes the year, month, day, hour, minute, and second, even if you don't supply a value for each of those fields.) Since you haven't listed the day, it uses the current day (29,30,31) and the symptom you are having somewhere (the link you posted worked for me as well) shows up when the current day doesn't exist in a month that you happen to be cycling through (Feb only had 28 days this year.) Use the 01 first day of the month in your strtotime statement "2011-02-01".

 

Also, you should almost never (by no means, not the least bit) ever put a query inside of a loop so that you execute it 30 times on a page to build a calendar. In fact that's likely why your page renders noticeably slow when you switch months. You should execute ONE query that gets all the rows you want (get all the rows for the selected year/month), in the order that you want them (order by classDate), then simply access the correct row(s) inside the loop as you iterate through the days in the month.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.