PHP - Validate if a date passes through another

0

It happens that I have a table where I keep records of things that are going to be done with a format: task, start date, end date

Example: Pruning the lawn, 2018-02-20, 2018-02-25

What I need is that you can not register in the BD tasks that are on that date or go through it.

The first case is already solved that is to report a task with a date greater than or equal to the start date and the end date is less than or equal to the record of the BD that is being evaluated:

Following the previous example: Prune the lawn, 2018-02-20, 2018-02-25

If I want to register a task, clean the pool with start date 2018-02-21, do not let it be done.

The detail is when I register a task like this:

Change the bulbs, 2018-02-15, 2018-02-30

I have not been able to identify how to detect that there are dates within this range.

The fuck I'm using is this:

//datos enviados desde el form
$start = date('Y-m-d', strtotime($start));
$end = date('Y-m-d', strtotime($end));

//obtengo las tareas registradas a futuro en la BD
    $actualNovelties = $em->getRepository('RocketSellerTwoPickBundle:Novelty')->getRegisterVacationsNoveltiesByUser($payroll);

        // creo un flag para validar si existen registros en ese rango
        $flagSame = 0;

        //valido si existen datos en la variable
        if($actualNovelties != null){

            //recorro cada una de las tareas
            foreach ($actualNovelties as $actualNovelties){

                //convierto a timestamp las fechas de los registros de la BD
                $dateBegin =strtotime($actualNovelties->getDateStart()->format('Y-m-d'));
                $dateEnd = strtotime($actualNovelties->getDateEnd()->format('Y-m-d'));


                //valido si la fecha de inicio enviada desde el form se cruza con las fechas de la BD
                if ((strtotime($start) >= $dateBegin) && (strtotime($start) <= $dateEnd))
                {
                    // si cumple la condición incrementa el flag
                    $flagSame++;
                }

                //valido si la fecha fin enviada desde el form se cruza con las fechas de la BD
                if ((strtotime($end) >= $dateBegin) && (strtotime($end) <= $dateEnd))
                {
                    // si cumple la condición incrementa el flag
                    $flagSame++;
                }

            }

        }
    
asked by Fernando León 16.02.2018 в 17:38
source

2 answers

2

I do not know what base you use, but you could use SQL to create a stored procedure that verifies it, like this:

 IF  NOT EXISTS (
        SELECT *
          FROM tabla_fechas
         WHERE fecha_nueva_inicio BETWEEN fecha_inicio AND fecha_fin
            OR fecha_nueva_fin BETWEEN fecha_inicio AND fecha_fin
    ) THEN 
    INSERT INTO tabla_fechas(fecha_inicio,fecha_fin)VALUES(fecha_nueva_inicio,fecha_nueva_fin)
END IF;

And call it from php

    
answered by 16.02.2018 в 18:07
0

As I commented the detail is in that I did not find how to validate if one date overlapped another, so I complemented the validations within the for each like this:

if((strtotime($start) <= $dateEnd) && (strtotime($end) >= $dateBegin)){
     $flagSame++;
}

What this last validation does is:

Check if the start date you are adding from the form is less than or equal to the end date of the data in the database, also check that the end date entered in the form is greater than or equal to the start date of the data. data from the BD.

Thanks !!!

    
answered by 17.02.2018 в 16:36