Jump to content

birth year


searls03

Recommended Posts

so I want to make a drop down list with the current year all the way down to 1900.  so lets say this year it lists 1900-2011, next year to 2012.  I want it to do this automatically without anybody changing it.  does anyone know how to do this?

Link to comment
Share on other sites

@Pikachu - This may be nitpicking, but PHP's date function is EXTREMELY slow. When you put it in a for loop, it will be executed at the start of each iteration, to verify the functions return value hasn't changed. Instead, use this

 

$year = date('Y');
for( $i = 1900; $i <= $year; $i++ ) // ... 

Link to comment
Share on other sites

nope came up with a different select box for each year.

 

So re-arrange to figure out why it is not working...that is like all of coding, trial and error till you get it right or give up.

 

<?php 
$dt = date('Y', strtotime( '- 1 year'));

echo '<select name="birthyear">';

for( $i = 1900; $i <= $dt; $i++ ) {
    echo '<option value="'.$i.'">';
    echo $i;
    echo '</option>';
}

echo '</select>';
?>

 

Link to comment
Share on other sites

@Pikachu - This may be nitpicking, but PHP's date function is EXTREMELY slow. When you put it in a for loop, it will be executed at the start of each iteration, to verify the functions return value hasn't changed. Instead, use this

 

$year = date('Y');
for( $i = 1900; $i <= $year; $i++ ) // ... 

 

No, you're absolutely right. I should have assigned the value to a variable first then used the variable.

Link to comment
Share on other sites

You should put the years in reverse order. 90% of your users are going to have a birth year in the last 55 years rather than the first 55 years, and that is pretty much an industry standard in my opinion. Also, the code provided previously would exclude the current year.

 

Here is my suggestion.

$thisYear = date('Y');
$firstYear = 1900;

echo '<select name="birthyear">';

for($year=$thisYear; $year>=$firstYear; $year++)
{
    echo "<option value=\"{$year}\">{$year}</option>\n";
}

echo '</select>';

Link to comment
Share on other sites

I'm sure you meant to decrement $year, rather than increment it . . .

 

Yes I did.

 

It may even be easier to use range() with a foreach() loop and still end up with the same result.

 

$years = range( date('Y'), 1900 );

Interesting, I would have thought that using range() would choke on that since the larger number is given as the first parameter and the step value (the optional third parameter) defaults to "1". I was surprised that it was "smart" enough to use a -1 step value.

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.