在PHP中的Unix时间戳范围内循环所有日期

在PHP中的Unix时间戳范围内循环所有日期,php,loops,strtotime,Php,Loops,Strtotime,假设您有两个Unix时间戳,如下所示: $startDate = 1330581600; $endDate = 1333170000; Start Loop Day Time Stamp: [Timestamp for the day within that loop] End Loop 我想循环该范围内的每一天,并输出如下内容: $startDate = 1330581600; $endDate = 1333170000; Start Loop Day Time Stamp:

假设您有两个Unix时间戳,如下所示:

$startDate = 1330581600;
$endDate = 1333170000;
Start Loop
   Day Time Stamp: [Timestamp for the day within that loop]
End Loop
我想循环该范围内的每一天,并输出如下内容:

$startDate = 1330581600;
$endDate = 1333170000;
Start Loop
   Day Time Stamp: [Timestamp for the day within that loop]
End Loop

我试图寻找某种类型的函数来实现这一点,但我甚至不确定这是否可行。

因为我喜欢DateTime、DateInterval和DatePeriod,以下是我的解决方案:

$start = new DateTime();
$end   = new DateTime();

$start->setTimestamp(1330581600);
$end->setTimestamp(1333170000);

$period = new DatePeriod($start, new DateInterval('P1D'), $end);

foreach($period as $dt) {
  echo $dt->format('Y-m-d');
  echo PHP_EOL;
}
起初,这似乎令人困惑,但这是一种非常合乎逻辑的方法

通过定义一个间隔为1天的周期的开始和结束(在中查找格式),然后您可以对其进行迭代。
最后,在每次迭代中,您都会得到一个可用于($t=$start;$t<$end;$t=strottime(“+1天,$t))的
DateTime::format(){
...
}

+1用于使用
DateTime
类家族,因为它们非常棒。