我正在准备一个节日网站。我有一个日期时间字段的节日日期表。 节日将介于“2013年6月/ 2013年”和“2013年4月7日”之间。 所以我创建的循环在这些答案在这里:如何从循环中获取所有日期
Schema::create('dates',function($table)
{
$table->increments('id');
$table->date('date');
$table->timestamps();
});
$starting_date = new DateTime('2013-03-06');
$ending_date = new DateTime('2013-04-06');
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($starting_date , $interval, $ending_date);
foreach($period as $dt)
{
DB::table('dates')->insert(array(
'date' => $dt
));
}
数据库被填充到4 4,循环不添加超过30天。 你能帮我找到一个修复失踪的日子吗?
PS:我用的替代while循环导致相同:
$starting_date = new DateTime('2013-03-06');
$ending_date = new DateTime('2013-04-07');
while($starting_date <= $ending_date){
DB::table('dates')->insert(array(
'date' => $starting_date
));
}
$starting_date->add(new DateInterval('P1D'));
}
你尝试过一段时间(的strtotime ($ starting_date)<= strtotime($ ending_date))? –
我应用了您的建议。 strtotime()期望参数1是字符串...我得到这个错误。 – ytsejam
将时间传递给'strtotime'时不要使用'DateTime'对象。例如:'$ starting_date ='2013-03-06'' – vanneto