Generate a drop down select box for year selection in Yii (PHP)
November 3rd, 2010 | by admin |
You can use this PHP code with or without the Yii framework to generate a select box of years. It’s useful for date of birth etc and especially brilliant for sites with minimum age requirements. The code only outputs valid years of birth for over 18′s only.
$yearNow = date("Y"); $yearFrom = $yearNow - 100; $yearTo = $yearNow - 18; $arrYears = array(); foreach (range($yearFrom, $yearTo) as $number) { $arrYears[$number] = $number; } $arrYears = array_reverse($arrYears, true); echo $form->dropDownList($model,'dateOfBirthYear',$arrYears);
If you use the above code without the Yii PHP framework then omit the echo line and output the array in your form manually
Home
HYGEN Web Design

April 5th, 2012 at 10:41 am
Thanks…it’s greats…
October 7th, 2011 at 4:16 am
Thanks Manuel,
Yes if you can skip the array_reverse then it will increase performance a little bit and save some code.
Dan
October 1st, 2011 at 7:30 pm
Nice function, saved me some time
By the way, if you change the order of parameters in range you can skip the array_reverse call later.
December 2nd, 2010 at 7:53 am
Cool
November 22nd, 2010 at 10:30 pm
have been using it for six months, one commercial project coming up!
November 21st, 2010 at 1:39 pm
Thanks Sum-Wai!
I just took a look at your blog… good stuff too! I haven’t written much about Yii yet – just been using it about a month. How long have you used Yii? Any commercial projects?
November 21st, 2010 at 8:15 am
Found your blog when I search for using the keyword “Yii Framework Malaysia”, nice blog, keep it up