Jump to content

[SOLVED] Dath Math...get previous year


RIRedinPA

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.