0

对于下面的代码,我想拥有当年的月份和接下来的 12 个月。我为它使用了一个循环,除了“一月”之外,一切都很好。我只是不知道出了什么问题。

for ($i = 0; $i <= 12; $i++) {
    $months[ucfirst(strftime("%B %G", strtotime( date( 'Y-m' )." +$i months")))] = ucfirst(strftime("%B %G", strtotime( date( 'Y-m' )." +$i months")));
    echo ucfirst(strftime("%B %G", strtotime( date( 'Y-m' )." +$i months")));
}

回声输出:

December 2015 Januari 2015 Februari 2016 Maart 2016 April 2016 Mei 2016 Juni 2016 Juli 2016 Augustus 2016 September 2016 Oktober 2016 November 2016 December 2016
4

2 回答 2

3

这样的事情呢?

<?php

$start = new DateTimeImmutable('first day of this month');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, 12);

foreach ($period as $date) {
    echo $date->format('F Y') . PHP_EOL;
}
于 2015-12-15T11:10:27.867 回答
2

您可以简单地使用:

$date = new DateTime('first day of this month');

for ($i = 0; $i < 13; $i++) {
    echo $date->format('F Y,');
    $date->modify('+1 month');
}

输出将是:

2015 年 12 月、2016 年 1 月、2016 年 2 月、2016 年 3 月、2016 年 4 月、2016 年 5 月、2016 年 6 月、2016 年 7 月、2016 年 8 月、2016 年 9 月、2016 年 10 月、2016 年 11 月、2016 年 12 月、

于 2015-12-15T11:15:21.007 回答