Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何填充这个PHP日期选择月数组?_Php_Arrays_Date_Padding - Fatal编程技术网

如何填充这个PHP日期选择月数组?

如何填充这个PHP日期选择月数组?,php,arrays,date,padding,Php,Arrays,Date,Padding,需要填充到以下位置:str_pad($Month,2,“0”,str_pad_LEFT) 但是如何/在哪里 /*******creates the month selection dropdown array****/ function createMonths($id='month_select', $selected=null) { /*** array of months ***/ $months = array(

需要填充到以下位置:str_pad($Month,2,“0”,str_pad_LEFT) 但是如何/在哪里

/*******creates the month selection dropdown array****/

 function createMonths($id='month_select', $selected=null)
    {
        /*** array of months ***/
        $months = array(
                1=>'Jan',
                2=>'Feb',
                3=>'Mar',
                4=>'Apr',
                5=>'May',
                6=>'Jun',
                7=>'Jul',
                8=>'Aug',
                9=>'Sep',
                10=>'Oct',
                11=>'Nov',
                12=>'Dec');

        /*** current month ***/
        $selected = is_null($selected) ? date('m') : $selected;

        $select = '<select name="'.$id.'" id="'.$id.'">'."\n";
        foreach($months as $key=>$mon)
        {
            $select .= "<option value=\"$key\"";
            $select .= ($key==$selected) ? ' selected="selected"' : '';
            $select .= ">$mon</option>\n";


        }
        $select .= '</select>';
        return $select;
    }

/********displays the month selection with current month selected/*******</br>
 echo createMonths('Month', date ('m')); 

/******DISPLAYS MONTH AS (4) INSTEAD OF REQUIRED (O4) --- @:( /*****</br>
echo $_POST['Month'];
/*******创建月份选择下拉列表数组****/
函数createMonths($id='month\u select',$selected=null)
{
/***月数***/
$months=数组(
1=>‘一月’,
2=>二月,
3=>“三月”,
4=>'Apr',
5=>“五月”,
6=>“六月”,
7=>七月,
8=>八月,
9=>“九月”,
10=>10月,
11=>11月,
12=>‘十二月’;
/***当月***/
$selected=为空($selected)?日期('m'):$selected;
$select=''。\n;
foreach($key=>$mon的月份)
{
$select.=“$mon\n”;
}
$select.='';
返回$select;
}
/********显示选中当前月份的月份选择/*******
月份(月),日期(米); /******将月份显示为(4)而不是必需的(O4)--@:(/*****
echo$_POST[“月];
好吧,假设这是您想要填充的值,它在这里:

$select .= "<option value=\"$key\"";
您可以使用或与格式字符串一起使用:

$m = sprintf('%02d', (int) $_POST['Month']);
当你说:

$select .= ">$mon</option>\n";
$select.=“>$mon\n”;
您应该使用str_pad函数,如下所示:

$select .= ">" . str_pad($mon, 2, "0", STR_PAD_LEFT) . "</option>\n";
$select.=“>”.str\u pad($mon,2,“0”,str\u pad\u LEFT)。“\n”;

这会尝试用零填充月份名称,但不会做任何事情,因为它们都不少于2个字符。Chad…我爱你;D我以前在该选择上尝试过,但我一直无法跟踪引号,最后填充了我的swearbox…非常感谢。
$select .= ">$mon</option>\n";
$select .= ">" . str_pad($mon, 2, "0", STR_PAD_LEFT) . "</option>\n";