简体   繁体   中英

Create months as options for select tag using php

I need to create a select tag with months as options using php
like this:

<option value='01'>JAN</option>
<option value='02'>FEB</option>

... and so on

here is my try - without success

<select>
<?php
$str = '{"01":"JAN","02":"FEB","03":"MAR","04":"APR","05":"MAY","06":"JUN","07":"JUL","08":"AUG","09":"SEP","10":"OCT","11":"NOV","12":"DEC"}';
$obj = new stdClass(json_decode($str));
foreach($obj->$key as $key=>$val){
    echo "<option value = '" . $key . "'>" . $val . "</option>";}
?>
</select>
<?php

    for($month = 1; $month <= 12; $month++) {
      echo '<option value="' . $month . '">' . strtoupper(DateTime::createFromFormat('!m', $month)->format('M')) . '</option>';
    }

Dont make life so complicated

<?php
$arr= ['01'=>'JAN', '02'=>'FEB', '03'=>'MAR', '04'=>'APR',
        '05'=>'MAY', '06'=>'JUN', '07'=>'JUL', '08'=>'AUG',
        '09'=>'SEP', '10'=>'OCT', '11'=>'NOV', '12'=>'DEC'
        ];

foreach($arr as $key=>$val){
    echo "<option value = '" . $key . "'>" . $val . "</option>";
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM