Skip to content

Instantly share code, notes, and snippets.

@hlashbrooke
Created July 12, 2013 11:00
Show Gist options
  • Save hlashbrooke/5983590 to your computer and use it in GitHub Desktop.
Save hlashbrooke/5983590 to your computer and use it in GitHub Desktop.
PHP function to easily generate a month select box
<?php
function month_select_box( $field_name = 'month' ) {
$month_options = '';
for( $i = 1; $i <= 12; $i++ ) {
$month_num = str_pad( $i, 2, 0, STR_PAD_LEFT );
$month_name = date( 'F', mktime( 0, 0, 0, $i + 1, 0, 0, 0 ) );
$month_options .= '<option value="' . esc_attr( $month_num ) . '">' . $month_name . '</option>';
}
return '<select name="' . $field_name . '">' . $month_options . '</select>';
}
?>
@marcoczen
Copy link

marcoczen commented Dec 4, 2017

Great snippet.

echo "<option $select value='$month_num'> $month_name</option>";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment