Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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 - Fatal编程技术网

Php 列出日期之间的所有年份

Php 列出日期之间的所有年份,php,Php,我希望使用PHP和Mysql数据库列出每年的日期间隔,代码如下: <?php $start = new DateTime('2010-12-02'); $start->modify('first day of this month'); $end = new DateTime('2016-05-06'); $end->modify('first day of next month'); $interval = DateInterval::createFromD

我希望使用PHP和Mysql数据库列出每年的日期间隔,代码如下:

<?php

$start    = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end      = new DateTime('2016-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 year');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    echo $dt->format("Y") . "<br>\n";
}

?>
错过2016年。

我该怎么做才能让2016年也有回报?它应该回到2010年

$start    = '2010-12-02';
$end      = '2016-05-06';
$getRangeYear   = range(gmdate('Y', strtotime($start)), gmdate('Y', strtotime($end)));
print_r($getRangeYear);
使用range函数获取测距编号列表、第一个参数的起始范围和第二个参数的结束范围。。

很简单:您在
2010-12-02
2016-05-06
之间进行迭代。生成的日期为:

  • 2010-12-02
    ->正常
  • 2011-12-02
    ->正常
  • 2012-12-02
    ->正常
  • 2013-12-02
    ->正常
  • 2014-12-02
    ->正常
  • 2015-12-02
    ->正常
  • 2016-12-02
    ->超过
    2016-05-06
    -停止算法
我不知道您到底想做什么,但似乎您应该更改结束日期。


<?php

    $start    = new DateTime('2010-12-02')->modify('+1 year');
    $start->modify('first day of this month');
    $end      = new DateTime('2016-05-06');
    $end->modify('first day of next month');
    $interval = DateInterval::createFromDateString('1 year');
    $period   = new DatePeriod($start, $interval, $end);

    foreach ($period as $dt) {
        echo $dt->format("Y") . "<br>\n";
    }

?>

只需编辑代码的第一行。一年后,我希望它仍能帮助其他人。
<?php

    $start    = new DateTime('2010-12-02')->modify('+1 year');
    $start->modify('first day of this month');
    $end      = new DateTime('2016-05-06');
    $end->modify('first day of next month');
    $interval = DateInterval::createFromDateString('1 year');
    $period   = new DatePeriod($start, $interval, $end);

    foreach ($period as $dt) {
        echo $dt->format("Y") . "<br>\n";
    }

?>