I came across an issue in generating a list of months for a select input. This particular issue will crop up on the 29th, 30th and 31st of any month.
The code I was using is below:
<select name="fld_month" id="fld_month">
<?php for ($x=1;$x<=12;$x++){ ?>
<option value="<?php echo $x; ?>"><?php echo date( 'M', mktime(0, 0, 0, $x) ); ?></option>
<?php } ?>
</select>
The mktime function highlighted in bold is the reason for the issue. For an example of the issue, I can use today. Today is the 30th January 2012. As I wasn’t passing the current day to the mktime function, it was automatically assuming that it should use todays date.
Unfortunately, there is no 30th of February, so my select list looked like this:
Jan
Mar
Mar
Apr
May
So in order to correct this (and the same issue that would occur on the 31st with any month with only 30 days), we updated the mktime function to use the first day of the month (see below).
<select name="fld_month" id="fld_month">
<?php for ($x=1;$x<=12;$x++){ ?>
<option value="<?php echo $x; ?>"><?php echo date( 'M', mktime(0, 0, 0, $x, 1) ); ?></option>
<?php } ?>
</select>
Do you have a better way of generating a list of months, date of birth or general date selectors? Let me know…