编程语言
首页 > 编程语言> > PHP:返回数组中两个日期之间的所有日期

PHP:返回数组中两个日期之间的所有日期

作者:互联网

参见英文答案 > I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?                                    8个
预期投入:

getDatesFromRange( '2010-10-01', '2010-10-05' );

预期产出:

Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )

解决方法:

您还可以查看DatePeriod课程:

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

哪个应该为您提供DateTime对象的数组.

迭代

foreach ($period as $key => $value) {
    //$value->format('Y-m-d')       
}

标签:date-range,php,datetime
来源: https://codeday.me/bug/20190911/1802943.html