Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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 $start_date = '2012-01-01'; $end_date = '2012-12-31'; $total_days = round(abs(strtotime($end_date) - strtotime($start_date)) / 86400, 0) + 1; if ($end_date >= $start_date) { for ($day = 0; $day < $tota

您上面的代码将使用一个额外的字符-您在这一行遗漏了一个
$

<?php

$start_date = '2012-01-01';
$end_date = '2012-12-31';
$total_days = round(abs(strtotime($end_date) - strtotime($start_date)) / 86400, 0) + 1;

if ($end_date >= $start_date)
{
  for ($day = 0; $day < $total_days; $day++)
  {
    echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {$day} days"));
                                 //     You missed the $ here ^
  }
}
echo“
”。日期(“Y-m-d”,STROTIME({$start_date}+{$day}天”); //^这个不见了
我将使用该类(以及and)来实现以下目的:

echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {$day} days"));
//                                                        ^ This was missing

@艾莉森·克里斯蒂安森:那你应该接受它作为答案。是的,我知道,它在最初的五分钟里不允许我这样做,我接着做了其他事情。谢谢你提醒我。
<?php

$start_date = '2012-01-01';
$end_date = '2012-12-31';
$total_days = round(abs(strtotime($end_date) - strtotime($start_date)) / 86400, 0) + 1;

if ($end_date >= $start_date)
{
  for ($day = 0; $day < $total_days; $day++)
  {
    echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {$day} days"));
                                 //     You missed the $ here ^
  }
}
echo "<br />" . date("Y-m-d", strtotime("{$start_date} + {$day} days"));
//                                                        ^ This was missing
$start_date = '2012-01-01';
$end_date = '2012-12-31';

$start    = new DateTime($start_date);
$end      = new DateTime($end_date);
$interval = new DateInterval('P1D'); // 1 day interval
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $day) {
    // Do stuff with each $day...
    echo $day->format('Y-m-d'), "\n";
}