我的形式有2场 - Time_from和Time_to日期时间加1天
现在我需要添加在我的数据库每天的条目(如果是孤单那些日子之间的差异) 前。 Time_from = 2013年8月26日08:00:00和Time_to = 2013年8月28日16:00:00 所以在我的数据库中,我应该得到3项
Time_from = 2013-08-26 08:00:00 and Time_to = 2013-08-26 16:00:00
Time_from = 2013-08-27 08:00:00 and Time_to = 2013-08-27 16:00:00
Time_from = 2013-08-28 08:00:00 and Time_to = 2013-08-28 16:00:00
因此,对于这个目的我已经做了方法,在那里我首先找到这两个日期之间的差异,然后我使用for循环,这将运行多天的差异这两个日期,最后我只是增加1天。
public function createOffer($offer)
{
$time_from = new DateTime($offer->time_from);
$time_to = new DateTime($offer->time_to);
$interval = $time_from->diff($time_to);
for ($counter = 0; $counter <= $interval->d; $counter++) {
$offer->time_from = $time_from->format('Y-m-d H:i:s');
$offer_time_to = new DateTime($time_from->format('Y-m-d'));
$offer_time_to->setTime($time_to->format('H'), $time_to->format('i')
, $time_to->format('s'));
$offer->time_to = $offer_time_to->format('Y-m-d H:i:s');
if ($offer->validate()) {
$offer->save();
}
date_modify($time_from, '+1 day');
}
}
由于某些原因,代码无法正常工作。
当我删除date_modify场只是第一天将被保存在我的数据库(猜for循环只运行一次,然后)
但在我的代码date_modify,只有最后一天被保存在数据库。
$ interval-> d没有“天”的区别,只有时间,在时代赋予这样的循环将只运行一次 – Anigel
等方面差异几天之间只显示i小时? – CreamYGEEK
http://phpfiddle.org/main/code/3r1-6hc你确定你不想要$ interval-> h而不是interval-> d – Anigel