我正在準備一個節日網站。我有一個日期時間字段的節日日期表。 節日將介於「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