php 一年中每个月的 for 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7020821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
for loop for each month of year
提问by scarhand
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.
我正在尝试在 PHP 中编写一个 for 循环以添加到 HTML<select>
标记下拉列表中,从而允许人们选择他们的出生月份。
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?
如何编写一个 for 循环来返回一年中每个月的名称?
回答by paxdiablo
You need to quote the value
key with:
您需要引用value
密钥:
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 knowwhat the values should be.
看来奇怪的,你将使所有这些不必要的电话date
和mktime
当你知道这些值应该是什么。
This array version has the same number of lines and it seems a lot clearer in intent (at least to me).
此数组版本具有相同的行数,并且其意图似乎更清晰(至少对我而言)。
回答by Basem Olimy
for($iM =1;$iM<=12;$iM++){
echo date("M", strtotime("$iM/12/10"));}
回答by Jeune
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>";
回答by Marciaum
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
回答by Rutvik kakadiya
PHP code to print out the name of each month:
打印出每个月的名称的 PHP 代码:
<?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,
}
?>
回答by Vladimir Lukyanov
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>';
}
回答by mpratt
I think you should fix the HTML part.
我认为您应该修复 HTML 部分。
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.phpfor valid timezones.
否则,您将收到 date() 和 mktime() 的 E_WARNING(如果您设置了 error_reporting(E_ALL))。查看http://www.php.net/manual/en/timezones.php了解有效时区。
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.
但是 for 循环就好了。