Enter dates in the database?

Asked

Viewed 236 times

3

How do I insert maximum days of a month via Insert into codeigniter? Ex:

data       |  campo2    camp3
01/01/2014 |
02/01/2014 |
03/01/2014 |
    .      |
    .      |
    .      |
31/01/2014 |
  • 1

    You want the last day of the month ?

  • vc want to insert into the bank by ex: from the first day until the last one which can be 28, 29, 30 and 31. this?

  • Exactly that!

1 answer

3


It is possible to generate these dates from the following loop.

Adapt the loop according to the codeigniter insertion method (I don’t remember what Active Record syntax looks like)

<?php 

$dataAtual = new DateTime();

// Imprime a data atual - Teste
echo $dataAtual->format('Y-m-d') . PHP_EOL . PHP_EOL;

// 't' retorna o último dia do mês de $dataAtual 
$ultimoDiaDoMes = $dataAtual->format('t');

for ($dia = 1; $dia <= $ultimoDiaDoMes; $dia++){
    $diaLoop = DateTime::createFromFormat('Y-m-d', $dataAtual->format('Y-m-') . $dia);

    // Aqui você insere o método para inserir no banco

    // Imprime a data do loop - Teste
    echo $diaLoop->format('Y-m-d') . PHP_EOL;
}

Will result in:

2014-06-09

2014-06-01
2014-06-02
2014-06-03
2014-06-04
2014-06-05
2014-06-06
...
2014-06-28
2014-06-29
2014-06-30
  • Thanks guy was that same, thank you very much!

Browser other questions tagged

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