Jump to content

fill dropdown with the years from 2000 to current


john_6767

Recommended Posts

i'm trying to fill a drop down with the years from year 2000 to two years ahead of the current year, ie for this year there would be the values: 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 as options in the drop down and then next year 2010 would appear..

i have been using the following code to display from two years back to one year ahead but cannot see how i can go from the year 2000 widthout having to update the code every year..

maybe someone can show me how to get the current year minus the year 2000 then loop through adding every year between?

[code]<select name="start_date" id="start_date" class="formDropDown"/>
<option value="<?php echo date("Y") - 2 ?>"><?php echo date("Y") - 2 ?></option>
<option value="<?php echo date("Y") - 1 ?>"><?php echo date("Y") - 1 ?></option>
<option value="<?php echo date("Y")?>"><?php echo date("Y") ?></option>
<option value="<?php echo date("Y") + 1 ?>"><?php echo date("Y") + 1 ?></option>
</select>[/code]
Link to comment
Share on other sites

cheers, i have just got this to work, its a bit messy, but incase it helps anyone else.. i'll now go check that link out and see if theres a better way to do it..
[code]
  <?php
  $DateParam = date("Y");
  $yearsBetween = $DateParam - 2000;
 
$i=0;
echo "<select name=\"start_date\" id=\"start_date\" class=\"formDropDown\"/>";
while($i<=$yearsBetween)
  {
  $yearVar = 2000 + $i;
  echo "<option value=\"" .$yearVar ."\">".$yearVar ."</option>";
  $i++;
  }
?>
<option value="<?php echo date("Y") + 1 ?>"><?php echo date("Y") + 1 ?></option>
<option value="<?php echo date("Y") + 2 ?>"><?php echo date("Y") + 2 ?></option>
</select>
[/code]
Link to comment
Share on other sites

Did you read the thread referred to in the other reply? That shows you how to do this very simply:
[code]<?php
echo '<select name="start_date" id="start_date" class="formDropDown"/>';
        $end_date = date('y') + 3;
        for($i=2000;$i<$end_date;$i++)
  echo '<option value="' .$i .'">'.$i .'</option>';
        echo '</select>';
?>[/code]

Ken
Link to comment
Share on other sites

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.