rcorlew Posted March 16, 2008 Share Posted March 16, 2008 I have a loop that I am trying to run through 12 times. It starts off just fine except that it runs through the loop for 1969 times. I have no idea what I am doing wrong as this very code works elsewhere but not for this. <?php $year = isset($_GET['year']) ? $_GET['year'] : date('Y'); $month = isset($_GET['month']) ? $_GET['month'] : date('m'); $first = mktime(0,0,0,$month,1,$year); // timestamp for first of the month $offset = date('w', $first); // what day of the week we start counting on $daysInMonth = date('t', $first); $monthName = date('F', $first); echo "<select name=\"month\" onChange=\"document.location.href=this.value\" class=\"topNavSelect\"> <option>$monthName</option>"; $x = 1; $y = 12; for( $i = $x; $i <= $y; $i++ ) { $thisMonth = strtotime("$year-$month-01 $i month"); $nMax = date('t', $thisMonth); $nDay = ($day > $nMax) ? $nMax : $day; list($y, $m) = explode('-', date('Y-m', $thisMonth)); $thisMonth = date('F', $thisMonth); $showMonth = "?year=$y&month=$m&day="; $myMonth = $thisMonth; echo "<option value=\"$showMonth\">$myMonth $i $y</option>"; } echo "</select>"; ?> I am sure that it is something simple that I have overlooked. Quote Link to comment https://forums.phpfreaks.com/topic/96348-need-help-with-a-for-loop/ Share on other sites More sharing options...
june_c21 Posted March 16, 2008 Share Posted March 16, 2008 $x = 1; $y = 12; for( $i = $x; $i <= $y; $i++ ) change to for( $i = 0; $i <= 12; $i++ ) Quote Link to comment https://forums.phpfreaks.com/topic/96348-need-help-with-a-for-loop/#findComment-493169 Share on other sites More sharing options...
rcorlew Posted March 16, 2008 Author Share Posted March 16, 2008 Superb, works perfectly. Thanks for the quick reply. Quote Link to comment https://forums.phpfreaks.com/topic/96348-need-help-with-a-for-loop/#findComment-493173 Share on other sites More sharing options...
discomatt Posted March 16, 2008 Share Posted March 16, 2008 You redefine $y here : list($y, $m) = explode('-', date('Y-m', $thisMonth)); for( $i = $x; $i <= $y; $i++ ) Now loops with the year that you played into $y Quote Link to comment https://forums.phpfreaks.com/topic/96348-need-help-with-a-for-loop/#findComment-493175 Share on other sites More sharing options...
rcorlew Posted March 16, 2008 Author Share Posted March 16, 2008 Hey if the "Topic Solved" tab was here, I would click it. Topic Solved. Quote Link to comment https://forums.phpfreaks.com/topic/96348-need-help-with-a-for-loop/#findComment-493177 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.