Month Dropdown in PHP

A simple way to generate an HTML select menu using PHP that lists the previous 12 months (or coming months). You can easily change the output of the labels and option values by adjusting the output format from the calls to date().

<select name="month">
<?php
  for ($i = 0; $i <= 12; ++$i) {
    $time = strtotime(sprintf('-%d months', $i));
    $value = date('Y-m', $time);
    $label = date('F Y', $time);
    printf('<option value="%s">%s</option>', $value, $label);
  }
  ?>
</select>
comments powered by Disqus