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 53EDIT3:
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;
}