TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Years select list (http://www.talkphp.com/general/1237-years-select-list.html)

Haris 09-26-2007 09:55 PM

Years select list
 
How can I echo 10 years from today in a select list?

For example.

2007
2008
2009
2010
2011
2012
2013
...

Haris 09-26-2007 10:07 PM

I researched on php range(); function and learned how to do it.

PHP Code:

                <p>
                    Start Date: 
                    <select name="startDateYear">
                        <option selected="selected" disabled="disabled">Year</option>
                        <?php
                        
foreach(range(date('Y'), date('Y')+10) as $iYears){
                            echo 
'<option>'.$iYears.'</option>';
                        }
                        
?>
                    </select>
                    <select name="startDateMonth">
                        <option selected="selected" disabled="disabled">Month</option>
                        <?php
                        
foreach(range(112) as $iMonths){
                            echo 
'<option>'.$iMonths.'</option>';
                        }
                        
?>
                    </select>
                    <select name="startDateDay">
                        <option selected="selected" disabled="disabled">Day</option>
                        <?php
                        
foreach(range(131) as $iDays){
                            echo 
'<option>'.$iDays.'</option>';
                        }
                        
?>
                    </select>
                </p>


Wildhoney 09-26-2007 10:09 PM

Something like this, perhaps. Less function calls.

PHP Code:

<select>
    <?php for($iYear date('Y'); $iYear <= date('Y') + 10$iYear++): ?>
    <option><?php echo $iYear?></option>
    <?php endfor; ?>
</select>


Salathe 09-26-2007 11:38 PM

Or, even less function calls (aka. why call date('Y') twice?):
PHP Code:

<select>
    <?php for ($iYear date('Y'), $iEndYear $iYear 10$iYear <= $iEndYear$iYear++) : ?>
        <option><?php echo $iYear?></option>
    <?php endfor; ?>
</select>

But yes, using the range is very helpful in many circumstances.

wiifanatic 09-29-2007 09:34 PM

This is more customizable (Change $yearsToGoBack):
PHP Code:

<select>
    <?php
    $yearsToGoBack 
10;
    
    for (
$i=0;$i<$yearsToGoBack;$i++) {
        
$year date('Y') - $i;
        echo 
'<option value="'.$year.'">'.$year.'</option>';
    }
    
?>
</select>

Enjoy!


All times are GMT. The time now is 09:18 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0