PHP 循环遍历月数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6221739/
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
PHP loop through months array
提问by kimion09
This should be easy but I'm having trouble...
这应该很容易,但我遇到了麻烦......
In PHP how can I echo out a select drop down box that defaults to the current month and has options for 8 months prior (even if it goes in the last year).
在 PHP 中,我如何回显一个默认为当前月份的选择下拉框,并且具有 8 个月之前的选项(即使它出现在去年)。
For example, for this month it would default to June and end at November.
例如,本月将默认为 6 月并在 11 月结束。
回答by deceze
$months = array();
for ($i = 0; $i < 8; $i++) {
$timestamp = mktime(0, 0, 0, date('n') - $i, 1);
$months[date('n', $timestamp)] = date('F', $timestamp);
}
Alternative for "custom" month names:
“自定义”月份名称的替代方案:
$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.');
$transposed = array_slice($months, date('n'), 12, true) + array_slice($months, 0, date('n'), true);
$last8 = array_reverse(array_slice($transposed, -8, 12, true), true);
To output an array of such months as dropdown is as simple as:
输出一个像下拉列表这样的月份数组很简单:
<select name="month">
<?php
foreach ($months as $num => $name) {
printf('<option value="%u">%s</option>', $num, $name);
}
?>
</select>
回答by Aleksandar Pavi?
If you need to print like select box options, try using this:
如果您需要像选择框选项一样打印,请尝试使用:
for($i=1;$i<13;$i++)
print("<option>".date('F',strtotime('01.'.$i.'.2001'))."</option>");
回答by SamT
$months = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July ',
'August',
'September',
'October',
'November',
'December',
);
$current = date('F');
$start = array_search($current, $months);
$toshow = array();
$total = 0;
for($i = $start; $total < 8; $i--)
{
if($i == 0)
{
$i = 12;
}
$toshow[] = $months[$i];
$total++;
}
var_dump($toshow);
Give that a shot, just dump out $toshow to your HTML.
试一试,只需将 $toshow 转储到您的 HTML 中即可。
回答by AR.
This is absolutely based on deceze's answer only months are sorted backwards and current month is selected
这绝对是基于 deceze 的回答,只有月份向后排序并选择当前月份
$curr_month = date('F',mktime(0, 0, 0, date('n')));
$months = array();
for ($i = 1; $i <= 8; $i++) {
$months[] = date('F', mktime(0, 0, 0, date('n') - $i, 1));
}
$months = array_reverse($months, true);
echo "<select>\n";
foreach($months as $key =>$value){
echo "<option value='$value'>$value</option>\n";
}
echo "<option value='$curr_month' selected='selected'>$curr_month</option>
</select>";
回答by Lawrence Cherone
some functions below... on page load will select current day, month, year + includes your required -8 months
下面的一些功能...在页面加载时将选择当前日期、月份、年份 + 包括您所需的 -8 个月
<form method="POST" action="">
<p><select size="1" name="day">
<?php formDay(); ?>
</select>-
<select size="1" name="month">
<?php formMonth(); ?>
</select>-
<select size="1" name="year">
<?php formYear(); ?>
</select> <input type="submit" value="Submit"></p>
</form>
<?php
//functions to loop day,month,year
function formDay(){
for($i=1; $i<=31; $i++){
$selected = ($i==date('n'))? ' selected' :'';
echo '<option'.$selected.' value="'.$i.'">'.$i.'</option>'."\n";
}
}
//with the -8/+8 month, meaning june is center month
function formMonth(){
$month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 8 months');
$end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 8 months');
while($month < $end){
$selected = (date('F', $month)==date('F'))? ' selected' :'';
echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
$month = strtotime("+1 month", $month);
}
}
function formYear(){
for($i=1980; $i<=date('Y'); $i++){
$selected = ($i==date('Y'))? ' selected' :'';
echo '<option'.$selected.' value="'.$i.'">'.$i.'</option>'."\n";
}
}
?>
回答by Faridul Khan
Another simple process to Get month names using a for loop and PHP date function.
使用 for 循环和 PHP 日期函数获取月份名称的另一个简单过程。
<?php
for($m=1; $m<=12; ++$m){
echo date('F', mktime(0, 0, 0, $m, 1)).'<br>';
}
回答by TheLastCodeBender
If you need to set this January to current month then this is how:
如果您需要将今年一月设置为当前月份,那么方法如下:
<?php
$startMonth = date('1');
$endtMonth = date('m');
for($m=$endtMonth ; $m<=$currentMonth; ++$m){
$months[$m] = strftime('%B', mktime(0, 0, 0, $m, 1));
echo $months[$m]."<br>";
}
?>