循环几天

use*_*330 -1 php datetime loops date

我希望返回特定范围之间的所有日子.
我的想法是将开始和结束日期转换为unix时间戳并循环遍历它们添加86400(一天中的几秒):

<?php
  $start = strtotime('2013-01-01');
  $end = strtotime('2013-02-01');

  for($i=$start; $i<=$end; $i+86400)
  {
     echo date("l, d.m.y", $i) . "\n";
  }
?>
Run Code Online (Sandbox Code Playgroud)

不幸的是,我只得到同一天:

Tuesday, 01.01.13
Tuesday, 01.01.13
Tuesday, 01.01.13
...
Run Code Online (Sandbox Code Playgroud)

sal*_*the 7

最佳做法是使用该DatePeriod课程.

$start = new DateTime('2013-01-01');
$end = new DateTime('2013-02-01');

foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $date) {
    echo $date->format("l, d.m.y\n");
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*rry 5

这是错的:

for($i=$start; $i<=$end; $i+86400)
Run Code Online (Sandbox Code Playgroud)

应该

for($i=$start; $i<=$end; $i+=86400)
Run Code Online (Sandbox Code Playgroud)

请注意原始代码的+=插入内容+.在你的代码中,你没有为变量赋值,只是执行没有结果的数学公式