user3733831
user3733831

Reputation: 2926

Create a dropdown for months using a php function.

I am using this function to create a dropdown for months.

function formMonth(){
  $month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 12 months');
  $end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 0 months');
  while($month < $end){
      $selected = (date('F', $month)==date('F'))? ' selected' :'';
      echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
      $month = strtotime("+1 month", $month);
  }
}

My problem is I need to use numeric values of 1 to 12 for option values.

At this time it use month name for value options like this.

<select size="1" name="month">
<option selected value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>

Can anybody tell me how to modify this function. Thank you.

Upvotes: 1

Views: 23352

Answers (9)

Arghadeeph Halder
Arghadeeph Halder

Reputation: 37

for($i=1; $i<=12; $i++)
{
echo '<option value="'.date('m', strtotime('2020-'.$i.'-01')).'">'.date('M', strtotime('2020-'.$i.'-01')).'</option>';  
}

Upvotes: 0

ShapCyber
ShapCyber

Reputation: 3652

I tried some of the answers here, and they have not given me the right solution to display an array of months. For example, the @Kausha Mehta solution outputted this one.

<select name="month" size="1">
<option value="7">July</option>
<option value="8">August</option>
<option value="10">October</option>
<option value="10">October</option>
<option value="12">December</option>
<option value="12">December</option>
<option value="1">January</option>
<option value="3">March</option>
<option value="3">March</option>
<option value="5">May</option>
<option value="5">May</option>
<option value="7">July</option>
</select>

Which seems to skip some months and duplicating some months.

But this work as expected.

        <select id="month" name="month" autocomplete='off'>
            <?php
    // Create a dropdown for months using a php foreach.
$monthArray  = range(1, 12);
//::::
   foreach ($monthArray as  $month):
   
       $monthCode = str_pad($month, 2, "0", STR_PAD_LEFT);

       $selected = ($monthCode === date('m')) ? 'selected' : '';  
//All months start with 1
        $date = new DateTime(date("Y-").$monthCode."-01");
       print '<option '.$selected.' value="'.$monthCode.'">'.$date->format('F').'</option>';
 endforeach;
            
    
    ?>
        </select>  

Upvotes: 0

Faiz Rasool
Faiz Rasool

Reputation: 1379

<select name="month">
<?php
foreach (['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] as $monthNumber => $month) {
    echo "<option value='$monthNumber'>{$month}</option>";
}
?>
</select>

Upvotes: 0

Sam
Sam

Reputation: 149

<div class="form-group">
          <label>Month:</label>
          <select id="month" name="month" class="form-control select2" style="width: 100%;">
            <option value=""></option>
            <?php
            $monthArray = range(1, 12);
            foreach ($monthArray as $month) {
              $monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
              $fdate = date("F", strtotime("2015-$monthPadding-01"));
                echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
            }?>
          </select>
        </div>

For more deail click here

Upvotes: 0

Ravi Hirani
Ravi Hirani

Reputation: 6539

Here is your answer:-

<?php
function months($selctedMonth='january'){

    $months='<select name="month" size="1">';
    for ($i = 12; $i > 0; $i--) {
        $time = strtotime(sprintf('-%d months', $i));   
        $label = date('F', $time); 
        $selctedM = strtolower($selctedMonth) == strtolower($label) ? 'selected' : '';
        $months.="<option value='$label'  $selctedM >$label</option>";
    }  
    $months.="</select>";
    return $months;
}

 echo months();    
?>

By default, January will be displayed as selected.

if you write <?php echo months('march'); ?> then march will be selected by default.

Upvotes: 1

Qirel
Qirel

Reputation: 26450

If you take a look at the documentation for date(), you'll see under "Format" that you can use n for numeric representation of months, from 1 to 12. (You can also use m for having leading zeroes (01-12)).

This means that you basically only have to change

echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";

to

echo '<option'.$selected.' value="'.date('n', $month).'">'.date('F', $month).'</option>'."\n";

in your current code, provided that it otherwise works as expected (all I did there was replace date('F', $month) in the values-attribute to date('n', $month).

Upvotes: 3

Kausha Mehta
Kausha Mehta

Reputation: 2928

Try below code:

<select name="month" size='1'>
    <?php
    for ($i = 0; $i < 12; $i++) {
        $time = strtotime(sprintf('%d months', $i));   
        $label = date('F', $time);   
        $value = date('n', $time);
        echo "<option value='$value'>$label</option>";
    }
    ?>
</select>

Upvotes: 5

Vigneswaran S
Vigneswaran S

Reputation: 2094

try this :your problem sloved.

<?php
function formMonth(){
  $month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 12 months');
  $end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 0 months');
  $val=1;
  echo "<select>";
  while($month < $end){

      $selected = (date('F', $month)==date('F'))? ' selected' :'';
      echo '<option'.$selected.' value='.$val.'>'.date('F', $month).'</option>'."\n";
      $month = strtotime("+1 month", $month);
       $val++;
  }
   echo "</select>";
}

formMonth();

?>

Upvotes: 1

nerdyDev
nerdyDev

Reputation: 376

<select size="1" name="month">
<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>

If thats what you want.

Enjoy!

Upvotes: 0

Related Questions