循环开始日期到结束日期

时间:2016-12-16 17:49:54

标签: php codeigniter-3

我有两列名称' start'并且'结束'类型日期。我想从头到尾遍历日期,并根据月份在表中插入一些数据。 我尝试了以下代码,但没有在我的表中插入任何内容。

这是我的代码:

$session_info = $this->db->get_where('session', array('componentId' => $session_id))->row_array();
$start = strtotime($session_info['start']);
$end = strtotime($session_info['end']);
$fee_classwise = $this->db->get_where('fee_conf', array('class_id' => $class_id))->result_array();

foreach ($fee_classwise as $row) {
    $feeInfo = $this->db->get_where('item', array('componentId' => $row['item_id']))->row_array();

    if($feeInfo['category3']=='ONCE') {
        $dataFee['studentFeeName'] = $feeInfo['itemName'];
        $dataFee['studentId']      = $student_id;
        $dataFee['sessionId']      = $running_year;
        $dataFee['itemId']         = $feeInfo['componentId'];
        $dataFee['amount']         = $feeInfo['salePrice'];
        $dataFee['month']          = date('F', $start);
        $dataFee['year']           = date('Y', $end);

        $this->db->insert('student_feeconfig', $dataFee);
    }

    if($feeInfo['category3']=='SESSION') {
        $session_start = $start;
        $session_end = $end;
        while($session_start < $session_end) {
            $dataFee['studentFeeName'] = $feeInfo['itemName'].'-'.date('M', $session_start);
            $dataFee['studentId']      = $student_id;
            $dataFee['sessionId']      = $session_id;
            $dataFee['itemId']         = $feeInfo['componentId'];
            $dataFee['amount']         = $feeInfo['salePrice'];
            $dataFee['month']          = date('F', $session_start);
            $dataFee['year']           = date('Y', $session_start);

            $this->db->insert('student_feeconfig', $dataFee);
            $session_start = strtotime('+4 month', $session_start);
        }
    }

    if($feeInfo['category3']=='MONTHLY') {
        $month_start = $start;
        $month_end = $end;
        while($month_start < $month_end) {
            $dataFee['studentFeeName'] = $feeInfo['itemName'] .'-'. date('M', $month_start);
            $dataFee['studentId']      = $student_id;
            $dataFee['sessionId']      = $session_id;
            $dataFee['itemId']         = $feeInfo['componentId'];
            $dataFee['amount']         = $feeInfo['salePrice'];
            $dataFee['month']          = date('F', $month_start);
            $dataFee['year']           = date('Y', $month_start);

            $this->db->insert('student_feeconfig', $dataFee);
            $month_start = strtotime('+1 month', $month_start);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

$start = strtotime($start);
$end = strtotime($end);        
$currentdate = $start;

while($currentdate <= $end)
{
    $cur_date = date('Y-m-d', $currentdate);

    $currentdate = strtotime('+1 days', $currentdate);

    //do what you want here                       
}

这是一个简单的例子,你可以尝试,祝你好运

答案 1 :(得分:0)

$startDate  = new DateTime('2016-12-01');
$interval   = new DateInterval('P1D'); // One day
$endData    = new DateTime('2016-12-31');
$period     = new DatePeriod($startDate, $interval, $endData);
foreach ($period as $dt) 
{
    echo $dt->format('d-m-y');
    echo "<br>";
    // Do whatever you want to do.
}