PHP 

Sometimes we need to select year value from drop-down and for that we hardcode the year values in HTML option fields. It may me cumbersome at the point when we want to add a new year value to the option field as the year value grows as the time progresses. So it is possible to dynamically generate the year values using PHP and we can use those generated year values in HTML select option tag.

You may also read Dynamically generate months in PHP

Here is the example how we can generate a range of year values in PHP technology.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <table>
            <tr>
                <th colspan="3">Usages of generate_years() function</th>
            </tr>
            <tr>
                <?php
                echo '<td>';
                echo '<br/><br/>default - last 10 years';
                echo '<br/>generate_years();';
                echo '<br/>';
                echo generate_years();
                echo '</td>';

                echo '<td>';
                echo 'generate from starting year 1990 to current year';
                echo '<br/>generate_years('year', 1990);';
                echo '<br/>';
                echo generate_years('year', 1990);
                echo '</td>';

                echo '<td>';
                echo 'generate from starting year 1990 to 2030';
                echo '<br/>generate_years('year', 1990, 2030);';
                echo '<br/>';
                echo generate_years('year', 1990, 2030);
                echo '</td>';
                ?>
            </tr>
        </table>

        <?php

        /**
         * dynamically generate year dropdown
         * @param int $startYear start year
         * @param int $endYear end year
         * @param string $id id of the select-option
         * @return html
         */
        function generate_years($id = 'year', $startYear = '', $endYear = '') {
            $startYear = (strlen(trim($startYear)) ? $startYear : date('Y') - 10);
            $endYear = (strlen(trim($endYear)) ? $endYear : date('Y'));

            if (!holds_int($startYear) || !holds_int($endYear)) {
                return 'Year must be integer value!';
            }

            if ((strlen(trim($startYear)) < 4) || (strlen(trim($endYear)) < 4)) {
                return 'Year must be 4 digits in length!';
            }

            if (trim($startYear) > trim($endYear)) {
                return 'Start Year cannot be greater than End Year!';
            }

            //start the select tag
            $html = '<select id="' . $id . '" name="' . $id . '">"n"';
            $html .= '<option value="">-- Year --</option>"n"';
            //echo each year as an option    
            for ($i = $endYear; $i >= $startYear; $i--) {
                $html .= '<option value="' . $i . '">' . $i . '</option>"n"';
            }
            //close the select tag
            $html .= "</select>";

            return $html;
        }

        function holds_int($str) {
            return preg_match("/^[1-9][0-9]*$/", $str);
        }
        ?>
    </body>
</html>

Here is the output when we run the generate_year file in browser.

generate years php

In the above screen-shot, the default – last 10 years means if we do not pass any parameter’s value then the function generate_years() generates last 10 years including current year; the generate from starting year 1990 to current year means when we pass first parameter(select tag’s id attribute’s value) value as ‘year’ and second parameter value as 1990 then the function generate_years(‘year’, 1990) generates year value from 1990 to 2016; the generate from starting year 1990 to 2030 means when we pass first parameter(select tag’s id attribute’s value) value as ‘year’, second parameter value as 1990 and third parameter value as 2030 then the function generate_years(‘year’, 1990, 2030) generates year value from 1990 to 2030.

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *