I need a system that registers several parcels of title in my DB Mysql I have this code below that registers and repeats the insert according to qtde of parcels informed, however happens that when choosing the date with day 30, arrives in February it skips the month and goes to March 2 .. where is the error? / p>
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, but I could not adapt it in codeigniter, in direct php I managed to work with him, even if I'm not mistaken he picks the last day of February, instead of picking up the next month, this is a monthly fee, can not stay for 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 {
}
}
}