scarhand
scarhand

Reputation: 4337

for loop for each month of year

I am trying to write a for loop in PHP to add to an HTML <select> tag dropdown, that allows people to pick their birth month.

Heres my code which isn't working:

<p>
    <label for="signup_birth_month">Birthday:</label>
    <select name="signup_birth_month" id="signup_birth_month">
        <option value="">Select Month</option>
        <?php for ($i = 1; $i <= 12; $i++){
            $month_name = date('F', mktime(0, 0, 0, $i, 1, 2011));
            echo '<option value="'.$month_name.'"'.$month_name.'></option>';
        }?>
    </select>
</p>

How do I write a for loop that returns the name of each month in year?

Upvotes: 3

Views: 53086

Answers (9)

Aj belduha
Aj belduha

Reputation: 11

Hey if you only needy for months and don't wants to make an array list of months you can try it

<select>

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

</select>

Upvotes: 0

Akintomiwa Opemipo
Akintomiwa Opemipo

Reputation: 419

You can use this function to loop through months based on the month format.

function months($month_format="F"){
    $months =  [];
    for ($i = 1; $i <=12; $i++) {
        $months[] = date($month_format, mktime(0,0,0,$i));
    }
    return $months;
}


var_dump(months());
//returns
array(12) {
  [0]=>
  string(7) "January"
  [1]=>
  string(8) "February"
  [2]=>
  string(5) "March"
  [3]=>
  string(5) "April"
  [4]=>
  string(3) "May"
  [5]=>
  string(4) "June"
  [6]=>
  string(4) "July"
  [7]=>
  string(6) "August"
  [8]=>
  string(9) "September"
  [9]=>
  string(7) "October"
  [10]=>
  string(8) "November"
  [11]=>
  string(8) "December"
}



var_dump(months("M"))
//returns
array(12) {
  [0]=>
  string(3) "Jan"
  [1]=>
  string(3) "Feb"
  [2]=>
  string(3) "Mar"
  [3]=>
  string(3) "Apr"
  [4]=>
  string(3) "May"
  [5]=>
  string(3) "Jun"
  [6]=>
  string(3) "Jul"
  [7]=>
  string(3) "Aug"
  [8]=>
  string(3) "Sep"
  [9]=>
  string(3) "Oct"
  [10]=>
  string(3) "Nov"
  [11]=>
  string(3) "Dec"
}

Personally, I use this function the loop into associative arrays and get html select and even target the selected value.

function html_selected($selected_option, $html_select_attributes, $options_and_values_array, $has_placeholder=false){
    $c = count($options_and_values_array);
    if ($has_placeholder){
        $c--;
    }
    
    if ($c===0){
        return "";
    }
    
    $select="<select $html_select_attributes>";
    foreach ($options_and_values_array as $k=>$v){
        if ((string)$k===(string)$selected_option){
            $f = "selected";
        }else{
            $f = "";
        }
        if (strpos($k,'"') !==-1){
            $dl = "'";
        }else{
            $dl = '"';
        }
        $select.="<option value=$dl".$k."$dl $f>$v</option>";    
    }
    $select.="</select>";
    
    return $select;
}

So all together, You have

$months = months();
echo html_selected("April","class='select-month'",array_combine($months,$months));

//returns
<select class='select-month'><option value='January' >January</option><option value='February' >February</option><option value='March' >March</option><option value='April' selected>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>


Upvotes: 0

Rutvik kakadiya
Rutvik kakadiya

Reputation: 87

PHP code to print out the name of each month:

<?php 
date_default_timezone_set('America/New_York'); 
for($i=1; $i<=12; $i++){ 
    $month = date('F', mktime(0, 0, 0, $i, 10)); 
    echo $month . ","; 
    // It will print: January,February,.............December,
}
?> 

Upvotes: 2

Vladimir Lukyanov
Vladimir Lukyanov

Reputation: 324

Very simple solution:

print '<option value="" disabled selected>Select month</option>';
for ( $i = 1; $i <= 12; $i ++ ) { 
    print '<option value="' . $i . '">' . date( 'F', strtotime( "$i/12/10" ) ) . '</option>'; 
}

Upvotes: 0

Marciaum
Marciaum

Reputation: 11

Very simple!

function getLocalMonthName($locale, $monthnum)
{
    setlocale (LC_ALL, $locale.".UTF-8");
    return ucfirst(strftime('%B', mktime(0, 0, 0, $monthnum, 1)));
}

print getLocalMonthName("pt_BR", 1); // returns Janeiro

Upvotes: 1

Basem Olimy
Basem Olimy

Reputation: 887

for($iM =1;$iM<=12;$iM++){
echo date("M", strtotime("$iM/12/10"));}

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 881153

You need to quote the value key with:

echo "<option value=\"" . $month_name . "\">" . $month_name . "</option>";

In addition, I'd probably prefer something like this:

$months = array("Jan", "Feb", "Mar", ..., "Dec");
foreach ($months as $month) {
    echo "<option value=\"" . $month . "\">" . $month . "</option>";
}

It seems bizarre that you would make all those unnecessary calls to date and mktime when you know what the values should be.

This array version has the same number of lines and it seems a lot clearer in intent (at least to me).

Upvotes: 13

mpratt
mpratt

Reputation: 1608

I think you should fix the HTML part.

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

Dont forget to set the default timezone before

date_default_timezone_set('Your/Timezone');

Or else you will get a E_WARNING for date() and mktime() (if you set error_reporting(E_ALL)). Take a look at http://www.php.net/manual/en/timezones.php for valid timezones.

Alternatively, you could also use something like

$i = 1;
$month = strtotime('2011-01-01');
while($i <= 12)
{
    $month_name = date('F', $month);
    echo '<option value="'. $month_name. '">'.$month_name.'</option>';
    $month = strtotime('+1 month', $month);
    $i++;
}

But the for loop is just fine.

Upvotes: 0

Jeune
Jeune

Reputation: 3538

Are you not seeing the month name get printed in the select box?

I think this should do the trick:

echo "<option value=\"" . $month_name . "\">" . $month_name . "</option>";

Upvotes: 1

Related Questions