Using date intervals on multiple inserts

Hello,

I have a form on which (among other values) the user has to select the amount of inserts that will be made and the date of the first insert.

I want that only the first insert has the selected date and the rest have intervals (which also will be selected by user)

Example: In the form I choose to insert 3 elements, and the interval to 5 days, starting by 10-apr-2016. Insert 2 should be with date 15-apr-2016 and insert 3 with date 20-apr-2016.

This is what I got in the Controller (of course I’ve not included the interval variable yet) I just want to know how do I add the interval from the base date.




    public function actionCreate()

    {

        $model = new Spraying();


        $post = Yii::$app->request->post();


        if (isset($post) && isset($post[$model->formName()]))

        {


            $cantidad = $post[$model->formName()]['cantidad'];

            $inicio = $post[$model->formName()]['fecha_inicio'];

            $aplicado = $post[$model->formName()]['aplicado'];

            $receta = $post[$model->formName()]['receta_id'];

            $programa = $post[$model->formName()]['programa_id'];

            $nombre = "spraying-";


            for ($i = 1; $i <= $cantidad; $i++) {

                //if (!isset($cantidad)) { continue; }

                Yii::$app->db->createCommand()->insert('spraying', [

                    'numero_spraying' => $nombre.$i,

                    'fecha_inicio' => $inicio,

                    'aplicado' => $aplicado,

                    'programa_id' => $programa,

                    'receta_id' => $receta,

                    ])->execute();

            }

            return $this->redirect(['index']);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }



Just calculate the next date in the foreach adding the interval to it, something like:





$date = $post[$model->formName()]['date']; //date from the form

$interval = $post[$model->formName()]['interval'];  //interval from the form


for ($i = 1; $i <= $cantidad; $i++) {

	Yii::$app->db->createCommand()->insert('spraying', [

    	'date'=$date,  // storing the date

    	'numero_spraying' => $nombre.$i,

    	'fecha_inicio' => $inicio,

    	'aplicado' => $aplicado,

    	'programa_id' => $programa,

    	'receta_id' => $receta,

    	])->execute();

	// calculate the new date as previous date + the intervall

	$date = date('Y-m-d', strtotime("$date +$interval days")); 

}




Side note:

Since you do not use a model you are missing data validation, do not forget to implement it (like interval as integer and not negative, valid date format and that is today or greater etc etc)

I assumed that the date come from the form in a format suitable for database storing like Y-m-d

I hope this is just a sample and finished code gets some validation because just think what could happen to your DB if $cantidad = 2147483647 :)

Anyway, take a look at batchInsert() so you can use only one query for this.

It’s better to prepare the list of rows to be inserted all at once.

As for the interval - you can get timestamp of the first date and then add 246060*days to get next date in a loop.

@Roberto Braga: Thank you, it works.

@Bizley: Yes, all the validations are in the Model, so all the inputs are limited to a range of numbers. And I’ll take a look at batchInsert. In the meantime this is solved.