imperium2335 Posted May 30, 2011 Share Posted May 30, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237843-month-not-working-php-bug/ Share on other sites More sharing options...
imperium2335 Posted May 30, 2011 Author Share Posted May 30, 2011 echo date("F, Y", strtotime("2011-02")); Gets me March, so there must be something wrong with my web hosts server? Quote Link to comment https://forums.phpfreaks.com/topic/237843-month-not-working-php-bug/#findComment-1222215 Share on other sites More sharing options...
wildteen88 Posted May 30, 2011 Share Posted May 30, 2011 working example here: www.caketopper.co.uk/2011/class-schedule.php There is nothing wrong with that calendar. Clicking the <- and -> buttons cycles through the months/years correctly. Quote Link to comment https://forums.phpfreaks.com/topic/237843-month-not-working-php-bug/#findComment-1222224 Share on other sites More sharing options...
PFMaBiSmAd Posted May 30, 2011 Share Posted May 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237843-month-not-working-php-bug/#findComment-1222258 Share on other sites More sharing options...
imperium2335 Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks for your replies, it is fixed now! I'm quite new to MySQL but this is doing what I want it to do now, so I think I'm going to leave it how it is. Quote Link to comment https://forums.phpfreaks.com/topic/237843-month-not-working-php-bug/#findComment-1222271 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.