Enter a Date Range

1

Good morning, I'm doing a system that needs to record the presence of users in an event, separating by date and time period. The part of the period was simple, but the date I got a problem, I need to get the date of the form that inserts the Start Date / Final Date and insert all the dates in that interval (besides the dates already inserted), to register the presence the user on each of the days for example. It would look like this:

   idevento  |        datainicial     |          datafinal
       1     | $_POST["datainicial"]  | $_POST["datafinal"]
       1     |         20/02/2016     |        25/02/2016

There the bank would look like this:

       |idevento|   datas  |   
       |   1    |20/02/2016|
       |   1    |21/02/2016| 
       |   1    |22/02/2016|
       |   1    |23/02/2016|
       |   1    |24/02/2016|
       |   1    |25/02/2016|

I found in some questions something similar but it was only the select one, which would help me in getting the bank dates to register the presence, but I needed to have the dates inserted in the bank to be able to register the presence in each one separated and generate a Final Presence Report, so I tried to adapt, but I did not get results.

EDIT 1: @Gabriel Herning I did the code this way, but I do not know if I'm sending it wrong to the bank or something. Should I create a new column to store the dates value right? In addition to the start date and end date

$dataInicial = $_POST['data'];
$dataFinal = $_POST['datafinal'];
$start = new DateTime($dataInicial);
$end = new DateTime($dataFinal);

$periodArr = new DatePeriod($start , new DateInterval('P1D') , $end);

foreach($periodArr as $period) {
    $end = $period->format('d/m/Y');
    $sql = mysqli_query($conn, "INSERT INTO evento (id, datas) VALUES('$id', '$end); 
}

The table has the fields idevent, datainicial, datefinal, and dates, do I need to pass which of them in the insert?

EDIT2: This is the current code

function presenca($datas){ 
        include "conectar.php";    
        $dataInicial = $_POST['data'];
        $dataFinal = $_POST['datafinal'];
        $start = new DateTime($dataInicial);
        $end = new DateTime($dataFinal);
        $periodArr = new DatePeriod($start , new DateInterval('P1D') , $end);
        foreach($periodArr as $period) {
            $period->format('d/m/Y');
            $sql = mysqli_query($conn, "INSERT INTO datas (datas) VALUES('$period')"); 
        }
        $conn = null;

}

if($_POST["pagina"] == "inserirEve"){   
    presenca($_POST['data'], $_POST['datafinal']);
}

<form method="POST" action="input.php">
<input type="hidden" name="pagina" value="inserirEve"/>
Data Inicial: <input type="date" name="data">
Data Final: <input type="date" name="datafinal">
<br>
<br>
<input type="submit" name="Inserir" value="Cadastrar">
</form>

And this error occurs here:

Catchable fatal error: Object of class DateTime could not be converted to string in C: \ xampp \ htdocs \ week \ input.php on line 53

EDIT3:

function presenca($datas){ 
            include "conectar.php";    
            $start = new DateTime($_POST['data']);
            $end = new DateTime($_POST['datafinal']);
            $periodArr = new DatePeriod($start , new DateInterval('P1D') , $end);
            foreach($periodArr as $period) {
                $period = $period->format('d/m/Y');
                $sql = mysqli_query($conn, "INSERT INTO datas (datas) VALUES('$period')"); 
            }
            $conn = null;
}
    
asked by anonymous 14.11.2016 / 14:14

2 answers

2

Use the Date \ Time library for this. Date \ Time is a native PHP library that gives you everything you need to work with dates. In it, you'll find the class DatePeriod .

With DatePeriod, you can calculate all dates within a period:

$start = new \DateTime('2016-02-20');
$end = new \DateTime('2016-02-25');
$periodArr = new \DatePeriod($start , new \DateInterval('P1D') , $end);

foreach($periodArr as $period) {
    echo $period->format('d/m/Y H:i:s').'<br />';
}

//data de término
echo $end->format('d/m/Y H:i:s');

Output:

20/02/2016 00:00:00
21/02/2016 00:00:00
22/02/2016 00:00:00
23/02/2016 00:00:00
24/02/2016 00:00:00
25/02/2016 00:00:00

A detail is about the end date, it should be considered as "cutoff date", ie it is not considered "within a period". Therefore, it is printed separately

If you only need to use the internal period, since it already has a beginning and an end, you can remove the start date using the DatePeriod :: EXCLUDE_START_DATE option:

$start = new \DateTime('2016-02-20');
$end = new \DateTime('2016-02-25');
$periodArr = new \DatePeriod($start , new \DateInterval('P1D') , $end , DatePeriod::EXCLUDE_START_DATE);

foreach($periodArr as $period) {
    echo $period->format('d/m/Y H:i:s').'<br />';
}

Output:

21/02/2016 00:00:00
22/02/2016 00:00:00
23/02/2016 00:00:00
24/02/2016 00:00:00
    
14.11.2016 / 14:52
1

You can use a function to return all dates between start and end before inserting into the database:

function createDateRangeArray($strDateFrom,$strDateTo) {
    // formato esperado: YYYY-MM-DD
    $aryRange=array();

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo>=$iDateFrom)
    {
        array_push($aryRange,date('Y-m-d',$iDateFrom));
        while ($iDateFrom<$iDateTo)
        {
            $iDateFrom+=86400;
            array_push($aryRange,date('Y-m-d',$iDateFrom));
        }
    }
    return $aryRange;
}

//chamando a função da seguinte forma
$dt_inicial = date('Y-m-d', strtotime($_POST["datainicial"]));
$dt_final = date('Y-m-d', strtotime($_POST["datafinal"]));
$array_datas = createDateRangeArray($dt_inicial , $dt_final);

foreach($array_datas as $data){
    //insere os dados no banco
}

Reference

    
14.11.2016 / 14:27