Forum Moderators: coopster
I am designing a form, and one of the fields is for date of birth.
I am having 3 select boxes. one for day, one for month and one for year.
im wanting to use a for loop to generate the options. so for the day i would need a for loop that goes from 1 to 31.
here is the current code i am using, which works fine. note this line (<?PHP for($day=1; $day<32; $day++) {?>)
<select name="dob_day" tabindex="4">
<option value="">00</option>
<?PHP for($day=1; $day<32; $day++) {?>
<option value="<?PHP echo sprintf("%02d",$day);?>"><?PHP echo sprintf("%02d",$day);?></option>
<?PHP }?>
</select>
however, when i ran this code. note this line (<?PHP for($day=1; $day=31; $day++) {?>)
<select name="dob_day" tabindex="4">
<option value="">00</option>
<?PHP for($day=1; $day=31; $day++) {?>
<option value="<?PHP echo sprintf("%02d",$day);?>"><?PHP echo sprintf("%02d",$day);?></option>
<?PHP }?>
</select>
the page took forever to load.
what is the reason for $day<32 being a lot quicker to complete than $day=31?
$days_in_months = array('Jan' => 31, 'Feb' => 28, ...);
if ($year == 2004 ¦¦ $year == 2008, ...) $days_in_months['Feb'] = 29; (For the index, use whatever values you use for your $month variable.)
Then:
for($day=1; $day <= $days_in_months[$month]; $day++) {
...
}