Jump to content

Novice need help with Year math


Drezzzer

Recommended Posts

I'm using this code to get me the variables to a select box on a register page:

$vars['user-birthdate-years'] = array(_VARS_USER_CHOOSE_ => "Please choose", _VARS_USER_NONE_ => _VARS_USER_UNDEF_); for ($i = date('Y') - 18; $i > date('Y') - 75; --$i) { $vars['user-birthdate-years'][$i] = "$i"; }

 

This gives me this result in the page source:

<select name="birthyear" class="date"><option value="-2" selected="selected">Please choose</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option><option value="1946">1946</option><option value="1945">1945</option><option value="1944">1944</option><option value="1943">1943</option><option value="1942">1942</option><option value="1941">1941</option></select>

 

Problem: When running the php code at the top it miss two years in the span as you see in the page source below, the year 1983 and 1984 is completely missing in the page source. Why is this happening, could the php code be corrected or is there a workaround in php for the missing years bug I seem to have found?

 

I really appreciate any help on this!

Edited by Drezzzer
Link to comment
Share on other sites

I'm going to pass on that because when I run your code to build the array I get

<select name="birthyear" class="date">
    <option value="-2">Please choose</option>
<option value="-1">N/A</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
<option value="1946">1946</option>
<option value="1945">1945</option>
<option value="1944">1944</option>
<option value="1943">1943</option>
<option value="1942">1942</option>
<option value="1941">1941</option>
</select>

The code I used

<?php
define("_VARS_USER_CHOOSE_", -2);
define("_VARS_USER_NONE_", -1);
define("_VARS_USER_UNDEF_", 'N/A');

$vars['user-birthdate-years'] = array(_VARS_USER_CHOOSE_ => "Please choose", _VARS_USER_NONE_ => _VARS_USER_UNDEF_); 
for ($i = date('Y') - 18; $i > date('Y') - 75; --$i) { 
    $vars['user-birthdate-years'][$i] = "$i"; 
}
$opts = '';
foreach ($vars['user-birthdate-years'] as $k=>$yr) {
    $opts .= "<option value='$k'>$yr</option>\n";
}    
?>
<select name="birthyear" class="date">
    <?=$opts?>
</select>
Link to comment
Share on other sites

Here's a different way of doing it and it has the same results. 

 

<?php
$userChoice = array(-2, "Please choose", -1, "N/A");
$year = date("Y");
$dob = range($year-18, $year-74);
?>

<select name="birthyear" class="date">
  <?php
    echo '<option value="' . $userChoice[0] . '">' . $userChoice[1] . '</option>' . "\n";
    echo '<option value="' . $userChoice[2] . '">' . $userChoice[3] . '</option>' . "\n";
    foreach ( $dob as $value ) {
      echo '<option value="'. $value . '">' . $value . '</option>' . "\n";
    }
  ?>
</select>

 

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.