我的表单有2个字段 - Time_from和Time_to
现在我需要在我的数据库中为每一天添加一个条目(如果这些日期之间存在差异)ex.Time_from = 2013-08-26 08:00:00和Time_to = 2013-08-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
Run Code Online (Sandbox Code Playgroud)
所以为了这个目的,我已经做了一个方法,我首先找到这两个日期之间的差异,然后我使用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()) …Run Code Online (Sandbox Code Playgroud)