Mysql Codeigniter Parcel System

Asked

Viewed 240 times

1

I need a system that registers multiple installments of title in my Mysql BD I have this code below that registers and repeats the Insert according to installments Qtde informed, but it happens that when choosing the date with day 30, arrives in February it jumps the month and goes to 2 March.. where the error is?

public function cad_despesa_funciona() {

    $nParcelas = '3';
    $dataPrimeiraParcela = '30/12/2017';
    //$dataemissao = date('Y-m-d');
    //$ndocumento = 'ndcu';
    //$titulo = 'titulo';
    //$valor = '500.00';

if($dataPrimeiraParcela != null && $nParcelas !=null){
    $dataPrimeiraParcela = explode('/',$dataPrimeiraParcela);
    $dia = $dataPrimeiraParcela[0];
    $mes = $dataPrimeiraParcela[1];
    $ano = $dataPrimeiraParcela[2];
} else {
    $dia = date("d");
    $mes = date("m");
    $ano = date("Y");
}

for($x = 1; $x <= $nParcelas; $x++){
     $dt_parcelas[$x] = date("Y-m-d",strtotime("+".$x." month",mktime(0, 0,0,$mes,$dia,$ano)));
    }
    var_dump($nParcelas);
    foreach ($dt_parcelas as $vencimento)
    {
        $dados = array(
            'titulo' => 'teste titulo',
            'ndocumento' => '123',
            'datavencimento' => $vencimento,
            'parcela' => '123',
            'formadepgto' => '2',
            'valor' => '50.00',
            'dataemissao' => '2017-10-25',
            'planodecontas' => '2',
            'status' => 'aberto',
            'parcelaatual' => '2'
        );
    $this->db->insert('sis_despesas', $dados);
    }
}

I have this other code tbm, only I could not adapt it in codeigniter, in direct php I managed to work with it, even if I’m not mistaken it takes the last day of February, instead of taking the next month, this is a monthly system, can not stay 1 month without.

    $parcelas = '3';
    $datavenc = '2017-12-30';
    $datavencimento = Array(date('Y' , strtotime($datavenc)), date('m' , strtotime($datavenc)), date('d' , strtotime($datavenc)));
    $data_array = Array($datavencimento[0], $datavencimento[1], $datavencimento[2]);
    $n = $data_array[1]-1;
    $v_i = $n;

for($i = 0; $i < $parcelas; $i++) {

    $v_i++;
    $v = strtotime ( '+'.$i.' month' , strtotime(implode("-", $data_array))) ;
    $nd = date ( 'Y-m-d' , $v );
    $p = explode("-", $nd);

if($v_i <= 12) {

    $base_mes = date("Y-m-t", strtotime($nd));
    $forma_data = $p[0].'-'.$v_i.'-01';
    $ultimo_dia_do_mes = date("Y-m-t", strtotime($forma_data));
    $b1 = explode("-", $base_mes); // EXPLODE DO BASE MES
    $b2 = explode("-", $ultimo_dia_do_mes); // EXPLODE DO ULTIMO DIA DO MÊS

if($b1[2]!=$b2[2]) {
        $gravarData = $b2[0] . "-" . $b2[1] . "-" . $b2[2] . " 00:00:00";
    } else {
        $gravarData = $b1[0] . "-" . $b1[1] . "-" . $data_array[2].  " 00:00:00";
    }
    //AQUI EU COLOCO O INSERT PARA O BD
    }
else {
} 
} 
}
  • Is your code complete? I’m missing where the $winning variable is loaded and the FOR add-on. All the problem is that if Voce give an Insert in the database of a day that does not exist in the month then the bank will put the difference of the days the biggest to the next month, that is, if Voce colcoar day 31/06/2018 it will insert in the bank day 01/07/2018.

  • It was incomplete, I didn’t realize it, I had put the code between <code>, now I saw that it was just giving spaces before q solved..

  • You can show a key to the farm $dados ? print_r($dados);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.