2016-12-16 91 views
0

我有兩列名稱類型日期的'開始'和'結束'。我想循環遍歷從開始到結束的日期,並根據月份在表中插入一些數據。 我嘗試了下面的代碼,但沒有插入我的表中。循環通過開始日期到結束日期

這裏是我的代碼:

$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); 
     } 
    } 
} 

回答

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      
} 

這是簡單的例子,你可以試試,運氣好

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. 
} 
相關問題