Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/153037-solved-dath-mathget-previous-year/
Share on other sites

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).

$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>

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>

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.

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.

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.