RIRedinPA Posted April 7, 2009 Share Posted April 7, 2009 Is there a simple way I could get previous years back to a certain year? For example: $currentyear = date("Y"); $prevyear = $currentyear while ($prevyear > 2007) { $prevyear = $prevyear - (1 year); } I'm building a site and have a drop down list which I want to go from the current year back to 2007. Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/ Share on other sites More sharing options...
cunoodle2 Posted April 7, 2009 Share Posted April 7, 2009 If its always 2007 why dont you just start at 2007 and count up until the current year? Here is code that will print a drop down menu of all years from 2 years previous of the current year to 15 years from the current year. <?php for ($jj = (date("Y") - 2); $jj < (date("Y") + 15); $jj++) { echo "\n\t <option "; if ($year_of_renewal == $jj) echo "SELECTED "; echo "value\"=".$jj."\">".$jj."</option>"; } ?> The above variable "$year_of_renewal" was used if you already have a variable set and you can "set" the drop down menu for the user (for example in the event of an "update your info" page or something along those lines). Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/#findComment-803767 Share on other sites More sharing options...
redarrow Posted April 7, 2009 Share Posted April 7, 2009 <?php $date_now=date("Y")-1; echo $date_now; ?> Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/#findComment-803768 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 $selyear=$_POST['year']; $backto=date('Y')-2; $now=date('Y'); for ($i=$backto;$i<=$now;++$i) { echo '<option value="'.$i.'"'.($selyear==$i ? ' selected' : '').'>'.$i.'</option>'; } That will make: <option value="2007">2007</option><option value="2008">2008</option><option value="2009" selected>2009</option> Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/#findComment-803772 Share on other sites More sharing options...
RIRedinPA Posted April 8, 2009 Author Share Posted April 8, 2009 Thanks for the tips everyone, here's what I ended up going with: <tr valign="top"> <td> <select name="thisyear" action="index.php" method=POST> <option value='null'>Select A Year</option> <?php //get current year $theyear = date("Y"); $optionlist = ""; while ($theyear > 2006) { $optionlist .= "<option value='$theyear'>$theyear</option>"; $theyear = $theyear - 1; } echo $optionlist; ?> </select> </td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/#findComment-804483 Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Sorry I misread that - I missed the bit where you said "back to" $selyear=$_POST['year']; $backto=date('Y')-2; $now=date('Y'); for ($i=$now;$i>=$backto;--$i) { echo '<option value="'.$i.'"'.($selyear==$i ? ' selected' : '').'>'.$i.'</option>'; } That will produce the same output as you're generating and if a year has been already set it'll be automatically selected as the default. Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/#findComment-804494 Share on other sites More sharing options...
RIRedinPA Posted April 8, 2009 Author Share Posted April 8, 2009 Sorry I misread that - I missed the bit where you said "back to" $selyear=$_POST['year']; $backto=date('Y')-2; $now=date('Y'); for ($i=$now;$i>=$backto;--$i) { echo '<option value="'.$i.'"'.($selyear==$i ? ' selected' : '').'>'.$i.'</option>'; } That will produce the same output as you're generating and if a year has been already set it'll be automatically selected as the default. $backto will only get you back two years, right? So what happens in 2010, 2011, etc. I probably wasn't clear, 2007 was the base date but it'll keep going forward. Quote Link to comment https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/#findComment-804720 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.