JM4
JM4

Reputation: 6788

PHP 'Years' array

I am trying to create an array for years which i will use in the DOB year piece of a form I am building. Currently, I know there are two ways to handle the issue but I don't really care for either:

1) Range:

I know I can create a year array using the following

<?php 
     $year = range(1910,date("Y"));
     $_SESSION['years_arr'] = $year;
?>

the problem with Point 1 is two fold: a) my function call shows the first year as 'selected' instead of "Year" as I have as option="0", and b) I want the years reversed so 2010 is the first in the least and shown decreasing.

My function call is:

PHP

<?php
    function showOptionsDrop($array, $active, $echo=true){
        $string = '';

        foreach($array as $k => $v){
            $s = ($active == $k)? ' selected="selected"' : '';
            $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";     
        }

        if($echo)   echo $string;
        else        return $string;
    }
?>

HTML

<table>
            <tr>
            <td>State:</td>
            <td><select name="F1State"><option value="0">Choose a year</option><?php showOptionsDrop($_SESSION['years_arr'], null, true); ?></select>
            </td>
            </tr>
</table>

2) Long Array

I know i can physically create an array with years listed out but this takes up a lot of space and time if I ever want to go back and modify.

ex: PHP

$years = array('1900'=>"1900", '1901'=>"1901", '1902'=>"1902", '1903'=>"1903", '1904'=>"1904", '1905'=>"1905", '1906'=>"1906", '1907'=>"1907", '1908'=>"1908", '1909'=>"1909", '1910'=>"1910", '1911'=>"1911", '1912'=>"1912", '1913'=>"1913", '1914'=>"1914", '1915'=>"1915", '1916'=>"1916", '1917'=>"1917", '1918'=>"1918", '1919'=>"1919", '1920'=>"1920", '1921'=>"1921", '1922'=>"1922", '1923'=>"1923", '1924'=>"1924", '1925'=>"1925", '1926'=>"1926", '1927'=>"1927", '1928'=>"1928", '1929'=>"1929", '1930'=>"1930", '1931'=>"1931", '1932'=>"1932", '1933'=>"1933", '1934'=>"1934", '1935'=>"1935", '1936'=>"1936", '1937'=>"1937", '1938'=>"1938", '1939'=>"1939", '1940'=>"1940", '1941'=>"1941", '1942'=>"1942", '1943'=>"1943", '1944'=>"1944", '1945'=>"1945", '1946'=>"1946", '1947'=>"1947", '1948'=>"1948", '1949'=>"1949", '1950'=>"1950", '1951'=>"1951", '1952'=>"1952", '1953'=>"1953", '1954'=>"1954", '1955'=>"1955", '1956'=>"1956", '1957'=>"1957", '1958'=>"1958", '1959'=>"1959", '1960'=>"1960", '1961'=>"1961", '1962'=>"1962", '1963'=>"1963", '1964'=>"1964", '1965'=>"1965", '1966'=>"1966", '1967'=>"1967", '1968'=>"1968", '1969'=>"1969", '1970'=>"1970", '1971'=>"1971", '1972'=>"1972", '1973'=>"1973", '1974'=>"1974", '1975'=>"1975", '1976'=>"1976", '1977'=>"1977", '1978'=>"1978", '1979'=>"1979", '1980'=>"1980", '1981'=>"1981", '1982'=>"1982", '1983'=>"1983", '1984'=>"1984", '1985'=>"1985", '1986'=>"1986", '1987'=>"1987", '1988'=>"1988", '1989'=>"1989", '1990'=>"1990", '1991'=>"1991", '1992'=>"1992", '1993'=>"1993", '1994'=>"1994", '1995'=>"1995", '1996'=>"1996", '1997'=>"1997", '1998'=>"1998", '1999'=>"1999", '2000'=>"2000", '2001'=>"2001", '2002'=>"2002", '2003'=>"2003", '2004'=>"2004", '2005'=>"2005", '2006'=>"2006", '2007'=>"2007", '2008'=>"2008", '2009'=>"2009", '2010'=>"2010");

$_SESSION['years_arr'] = $years_arr;

Does anybody have a recommended idea how to work - or just how to simply modify my existing code?

Thank you!

Upvotes: 31

Views: 46898

Answers (12)

Licifer
Licifer

Reputation: 1

<select>
<?php
for ($i=1930; $i <2024 ; $i++) {?>
<option value="<?php echo $i ?>"> <?php echo $i; ?> </option>
<?php
}
?>
</select>

Upvotes: -1

mymonaco2000
mymonaco2000

Reputation: 41

for ($count = date('Y'); $count >= 1910; $count--) 
    echo '<option value="' . $count . '">' . $count . '</option>';

Upvotes: 4

pfwd
pfwd

Reputation: 1168

Extending Brenton Alker's answer; if you need to add something to the start of the array you could do something like this:

$keys =  array_merge(['any'], range(date("Y"), 1910));
$values = array_merge(['Any'], range(date("Y"), 1910));
$choice = array_combine($keys, $values);

This will add the key/value of any/Any to the start of the array whilst keeping all the subsequent keys in place

Upvotes: 0

user3292639
user3292639

Reputation: 65

you can simply use PHP foreach() function!

example:

$years_now = date("Y");

echo '<select name="y">';   
echo '<option value="" disabled>Now:</option>';
            echo '<option value="'.$years_now.'" selected="selected">'.$years_now.'</option>';
            echo '<option value="" disabled>Other:</option>';               
            foreach (range($years_now, 1910) as $years) {       
                echo '<option value="'.$years.'">'.$years.'</option>';          
            }           
        echo '</select>';

Upvotes: 0

Brenton Alker
Brenton Alker

Reputation: 9072

Not sure why you're using the session for this, but generating the array can be done with the array_combine function.

$years = array_combine(range(date("Y"), 1910), range(date("Y"), 1910));

Reversing the parameters to range will give you a descending array and array_combine will use the first array as the keys and the second as the values, giving the array(1910 => 1910, ...); map you're after.

Upvotes: 65

Allen Mak
Allen Mak

Reputation: 51

Why do you need to store the array into the session?

Based on your use case, you do not need to use the array to store data beforehand.

define('DOB_YEAR_START', 1900);

$current_year = date('Y');

for ($count = $current_year; $count >= DOB_YEAR_START; $count--)
{
    print "<option value='{$count}'>{$count}</option>";
}

Upvotes: 3

Anurag
Anurag

Reputation: 141869

reverse the numbers in the range to get years in descending order

$years = range(2010, 1900); // => [2010, 2009, 2008, ... ]

use date('Y') instead of hard-coding the current year

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

append an option "Select year" at the beginning

array_unshift($years, "Select year");

And finally why have a select drop-down for year or date of birth at all? It might be prevalent but it's super irritating. A simple text-box with validations, something like dd/mm/yyyy or mm/dd/yyyy is way better. Having drop-downs for date, month, and year only means that a user is allowed to select valid values and not necessarily correct values if they find it convoluted just to input a date. Moreover, since users can still enter junk values by modifying the DOM, validations need to be done on the server side. If validations are being done on the server-side, might as well just offer them a simple text box.

Also drop-downs make client-side coding complex. For example, if Feb and a leap year are selected, then a days dropdown should contain 29 days, otherwise 28.

Upvotes: 33

Ben Rowe
Ben Rowe

Reputation: 28691

I'd go with option #1. However all you need to do is prepend a value to the array before you print it:

<?php 
$years = $_SESSION['years_arr'];
// reverse the year list
rsort($years);
// prepend the 'choose a year' value to the array
array_unshift($years, 'Choose a year');
?>

<td><select name="F1State"><?php showOptionsDrop($years, null, true); ?></select>

Upvotes: 0

Ben
Ben

Reputation: 16533

Why don't you do this on your first approach

    foreach($array as $k){
        $string .= '<option value="'.$k.'">'.$k.'</option>'."\n";     
    }

Semantically, and functionally, speaking it's better to use the year as value than an int.

Also, use drawnonward method to get an inverted array so the year 2010 is the first, and default, value on your list.

Upvotes: 1

ortuna
ortuna

Reputation: 31

part 2:

$years = array();
for($i = 1900;$i<= 2010;$i++)
    $years["$i"] = $i;

Upvotes: 0

Tim
Tim

Reputation: 813

If just reversing the order of your year array is the goal you can use the array_reverse() function.

http://www.php.net/manual/en/function.array-reverse.php

Upvotes: 0

drawnonward
drawnonward

Reputation: 53659

In option 1 add:

$year = array_reverse( $year );

or just use:

$year = range( date("Y") , 1910 );

Upvotes: 2

Related Questions