I'm trying to change the year on this credit card form. Right now it only shows 2014-2022. I"m trying to change the year formula to start at 2013.
<?php $year = date ("Y"); $i = 1;?>
<option value="" selected="selected">Please Select a Year</option>
<?php while($i < 10) { $year += 1; $i++;?>
<option value="<?=$year?>">
<?=$year?>
</option>
<?php } ?>
Move your counter to the end of your loop:
<?php $year = date ("Y"); $i = 1;?>
<option value="" selected="selected">Please Select a Year</option>
<?php while($i < 10) { ?>
<option value="<?=$year?>">
<?=$year?>
</option>
<?php $year += 1; $i++; } ?>
$i
needs to start at 0 so that the first year is 2013 + 0, not 2013 + 1.
<?php $year = date ("Y"); $i = 0;?>
Better still, use a for
loop:
<? for ($year = date("Y"); $year < date("Y") + 10; ++$year) { ?>
<option value="<?= $year ?>"><?= $year ?></option>
<? } ?>
It's just use a for loop with the year as the counter.
<option value="" selected="selected">Please Select a Year</option>
<?php for($year = (int)date("Y"); $year < ((int)date("Y"))+10; $year++): ?>
<option value="<?=$year?>"><?=$year?></option>
<?php endfor; ?>
</option>
An other approach;
$years = range(date('Y'), date('Y') + 10);
while ($year = array_shift($years)) {
printf("<option value=\"%d\">%d</option>
", $year, $year);
}
// or
$years = range(date('Y'), date('Y') + 10); $i = 0;
while ($year =@ $years[$i++]) {
printf("<option value=\"%d\">%d</option>
", $year, $year);
}