Loop through start date to end date

2.2k Views Asked by At

I have two columns name 'start' and 'end' of type date. I want to loop through the date from start to end and insert some data in a table according to month. I tried the following code but nothing inserted in my table.

Here is my code:

$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

There are 2 best solutions below

0
On
$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                       
}

this is simple example you can try, good luck

0
On
$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.
}