I want to make different tables from 2 variables: start date and end date.
Example: input start= 05/07/2022 input end= 07/07/2022
so, when I submit the form I want this result:
ddbb: id:01 05/07/2022 id:02 06/07/2022 id:03 07/07/2022
I tried this but I got the dates displace, like this:
id:01 06/07/2022 id:02 07/07/2022 id:03 08/07/2022
This is the code:
<?php require('db.php'); if (isset($_REQUEST['DIACOMIENZO'])){ $DIACOMIENZO = $_REQUEST['DIACOMIENZO']; $DIAFIN = $_REQUEST['DIAFIN']; while (strtotime($DIACOMIENZO) <= strtotime($DIAFIN)) { $stmt = $con->prepare("INSERT INTO DIASHORARIOS (DIA) VALUES (?)"); $stmt->bind_param("s", $DIACOMIENZO); $DIACOMIENZO = date ("Y-m-d", strtotime("+1 days", strtotime($DIACOMIENZO))); $stmt->execute(); $stmt->close(); } ?>
Advertisement
Answer
It’s because you increased the date by 1 day even before you inserted it into the database. If you want the first value to be the start date, insert it, and then increment it afterwards.
For example:
while (strtotime($DIACOMIENZO) <= strtotime($DIAFIN)) { $stmt = $con->prepare("INSERT INTO DIASHORARIOS (DIA) VALUES (?)"); $stmt->bind_param("s", $DIACOMIENZO); $stmt->execute(); $stmt->close(); $DIACOMIENZO = date ("Y-m-d", strtotime("+1 days", strtotime($DIACOMIENZO))); }