insert date sequence in sequence in bank

1

I would like to insert the date on it to add + 1 day and insert again.

I have this script but it returns me random date:

date_default_timezone_set("Brazil/East");

$data = '2017-01-02';  
$data_fim='2017-01-09';

echo '<br />';
//faz a repeticao
for($i = 0; $i < $data_fim; $i++)
{
    // Soma 1 DIA

    echo $data = date('Y-m-d', strtotime($i . " days", strtotime($data)));

    echo '<br />';
}

echo '<br />';

type like this:

2017-01-02  
2017-01-03  
2017-01-05  
2017-01-08  
2017-01-12  
2017-01-17  
2017-01-23  
2017-01-30  
2017-02-07  
2017-02-16  
2017-02-26  
2017-03-09  
2017-03-21  
2017-04-03  
2017-04-17  
2017-05-02  
2017-05-18  
2017-06-04  
2017-06-22  
2017-07-11  
2017-07-31  
2017-08-21  

Can anyone tell me what I'm doing wrong?

    
asked by anonymous 18.03.2017 / 02:21

1 answer

1

Do you agree that comparing an integer value $i with a string $data_fim does not make any sense?

If you want to work with date ranges, you can use the PHP DateTime native class. First, setting the start and end dates:

$data_inicio = new DateTime('2017-01-02'); 
$data_fim = new DateTime('2017-01-09');

The DateTime class has a method called add to add dates, accepting an instance of DateInterval "as parameter, which will set the time interval to be added. To set a period of 1 day, you can do:

$interval = new DateInterval("P1D");
  

The parameter P1D that is responsible for setting the period. P means period and 1D the 1 day interval.

To go through the dates, then just loop:

for ($i = $data_inicio; $i <= $data_fim; $i->add($interval))
{
    echo $i->format("Y-m-d") . PHP_EOL;
}

The output of the program will be:

2017-01-02
2017-01-03
2017-01-04
2017-01-05
2017-01-06
2017-01-07
2017-01-08
2017-01-09

See working at Repl.it or Ideone .

    
18.03.2017 / 03:13