以下代码在浏览器上运行良好:
<?php
$start = (new DateTime('2010-12-02'))->modify('first day of this month');
$end = (new DateTime('2012-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("Y-m"). "<br>\n";
}
?>
并在浏览器上提供以下结果:
2010-12 2011-01 2011-02 2011-03 2011-04 2011-05 2011-06 2011-07 2011-08 2011-09 2011-10 2011-11 2011-12 2012-01 2012-02 2012-03 2012-04 2012-05
在phrunner中,我试图将上述数据(多个数据)插入mysql中的一行,但仅插入第一个数据(2010-12):< / p>
...
foreach ($period as $dt)
{
$sql = "INSERT INTO table2 (payment) values ('".$data->format("Y-m")."')";
CustomQuery($sql);
}
?>