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 为以后每个月创建一个数组_Php_Date - Fatal编程技术网

Php 为以后每个月创建一个数组

Php 为以后每个月创建一个数组,php,date,Php,Date,我找不到一种方法来创建数组后,每个月目前的一个 <?php $month = 07; $year = 2016; $end = date('Y'); for($i=0;$i<=$end-$year;$i++){ $from = 1; if($i==0)

我找不到一种方法来创建数组后,每个月目前的一个

            <?php 
                $month = 07; 
                $year = 2016;
                $end = date('Y');
                for($i=0;$i<=$end-$year;$i++){
                    $from = 1;
                    if($i==0)
                        $from = $month;


                    for($y=$from;$y<=12;$y++){
                        if($year==date('Y') && $y > date('m'))
                            break;

                        $a = $year+$i.'-'.$y;
                        $months[$a] = $a;
                    }
                }
                krsort($months);
            ?>
            <?php echo Html::dropDownList('month_year',$month_year,$months,array('class'=>'form-control'));?>

我想得到一个类似这样的下拉列表

2016-09

2016-08

2016-07


每到一个月。起始日期应为2016-07年。最大值应该是当前月份。

只需创建开始和结束日期,并循环使用它们:

$start    = (new DateTime('next month'))->modify('first day of this month');
$end      = (new DateTime('+24 months'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y-m") . "<br>\n";
}
$start=(新的日期时间(“下个月”)->modify(“本月的第一天”);
$end=(新日期时间(“+24个月”)->modify(“+24个月”);
$interval=DateInterval::createFromDateString('1个月');
$period=newdateperiod($start、$interval、$end);
foreach(期间为$dt){
echo$dt->格式(“Y-m”)。“
\n”; }
我使用和,因为这是最干净的方式来做这件事,并处理DST和闰年本机


只需创建一个开始和结束日期并循环:

$start    = (new DateTime('next month'))->modify('first day of this month');
$end      = (new DateTime('+24 months'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y-m") . "<br>\n";
}
$start=(新的日期时间(“下个月”)->modify(“本月的第一天”);
$end=(新日期时间(“+24个月”)->modify(“+24个月”);
$interval=DateInterval::createFromDateString('1个月');
$period=newdateperiod($start、$interval、$end);
foreach(期间为$dt){
echo$dt->格式(“Y-m”)。“
\n”; }
我使用和,因为这是最干净的方式来做这件事,并处理DST和闰年本机


谢谢你的帮助,我刚刚将“开始”修改为“固定”,将“结束”修改为“本月”。谢谢你的解决方案:)谢谢你对我的帮助,我刚刚修改了开始是固定的,结束是本月。感谢您提供的解决方案:)