Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 一年中每个月的for循环_Php_Date_For Loop - Fatal编程技术网

Php 一年中每个月的for循环

Php 一年中每个月的for循环,php,date,for-loop,Php,Date,For Loop,我试图用PHP编写一个for循环,添加到HTML标签下拉列表中,允许人们选择出生月份 以下是我的代码,它不起作用: <p> <label for="signup_birth_month">Birthday:</label> <select name="signup_birth_month" id="signup_birth_month"> <optio

我试图用PHP编写一个for循环,添加到HTML
标签下拉列表中,允许人们选择出生月份

以下是我的代码,它不起作用:

<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>

生日:
选择月份


如何编写一个for循环,返回一年中每个月的名称?

您是否看到在选择框中打印了月份名称

我认为这应该起作用:

echo "<option value=\"" . $month_name . "\">" . $month_name . "</option>";
echo”“$月名。"";

您需要引用
键:

echo "<option value=\"" . $month_name . "\">" . $month_name . "</option>";
echo”“$月名。"";
此外,我可能更喜欢这样的东西:

$months = array("Jan", "Feb", "Mar", ..., "Dec");
foreach ($months as $month) {
    echo "<option value=\"" . $month . "\">" . $month . "</option>";
}
$months=数组(“一月”、“二月”、“三月”、“十二月”);
foreach(月份为$month){
回音“$month.”;
}
当您知道值应该是什么时,您会对
date
mktime
进行所有那些不必要的调用,这似乎很奇怪


这个数组版本有相同的行数,而且它的意图似乎更加清晰(至少对我来说)。

我认为您应该修复HTML部分

echo '<option value="'.$month_name.'">'.$month_name.'</option>';
否则,您将收到date()和mktime()的E_警告(如果您设置了error_reporting(E_ALL))。 请查看有效的时区

或者,您也可以使用

$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++;
}
$i=1;
$month=strotime('2011-01-01');
而($i
for($iM=1;$iM非常简单

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
非常简单的解决方案:

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>'; 
}
打印“选择月份”;

对于($i=1;$iPHP代码,用于打印每个月的名称:

<?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,
}
?> 

嘿,如果你只需要几个月的时间,而不想制作一个月的数组列表,你可以试试它

<select>

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

</select>


您可以使用此函数根据月份格式循环月份

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"
}

功能月($month\u format=“F”){
$months=[];
对于($i=1;$i)
字符串(7)“一月”
[1]=>
字符串(8)“二月”
[2]=>
字符串(5)“三月”
[3]=>
字符串(5)“四月”
[4]=>
字符串(3)“可以”
[5]=>
字符串(4)“六月”
[6]=>
字符串(4)“七月”
[7]=>
字符串(6)“八月”
[8]=>
字符串(9)“9月”
[9]=>
字符串(7)“10月”
[10]=>
字符串(8)“11月”
[11]=>
字符串(8)“12月”
}
var_转储(月(“M”))
//返回
阵列(12){
[0]=>
字符串(3)“Jan”
[1]=>
字符串(3)“二月”
[2]=>
字符串(3)“三月”
[3]=>
字符串(3)“Apr”
[4]=>
字符串(3)“可以”
[5]=>
字符串(3)“Jun”
[6]=>
字符串(3)“七月”
[7]=>
字符串(3)“Aug”
[8]=>
字符串(3)“Sep”
[9]=>
字符串(3)“十月”
[10]=>
字符串(3)“11月”
[11]=>
字符串(3)“Dec”
}
就我个人而言,我使用此函数将循环转换为关联数组,并获得html选择,甚至针对选定的值

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;
}
函数html\u selected($selected\u option,$html\u select\u attributes,$options\u and\u values\u array,$has\u placeholder=false){
$c=计数($options\u和\u values\u数组);
如果($has\u占位符){
$c--;
}
如果($c==0){
返回“”;
}
$select=“”;
foreach($k=>v的选项和值数组){
if((字符串)$k==(字符串)$selected\u选项){
$f=“选定”;
}否则{
$f=“”;
}
如果(strpos($k,“')!=-1){
$dl=“”;
}否则{
$dl=“”;
}
$select.=“$v”;
}
$select.=“”;
返回$select;
}
总之,你有

$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>


$months=months();
echo html_selected(“四月”,“class='select-month'”,数组_combine($months,$months));
//返回
1月2日3月4月5日6月9日10月10日11月12日

A
printf()
看起来会更好,即
printf('%1$s',$month\u name);
@Phil不知道printf。谢谢你的提示!:)只有代码的答案质量很低,需要审查,你的代码没有错,所以我通过清理代码并添加一些文本来挽救它。如果你想在Stackoverflow上赢得声誉,就必须学习英语,并在每段代码上至少写几句话,说明它的作用和如何回答e问题。
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;
}
$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>