Date range loop into bbdd

vLife

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();
    
    }

?>
ADyson

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)));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

generate date range to be used in a loop

Loop through a date range with JavaScript

How do I loop through a date range?

PHP - Check Date Range/Availability in foreach loop

Loop through rows and exploding date range

Loop through date range with a monthly step

Is there way start a loop in a certain date range

Creating date range for each loop in a date object array in js

What does BBDD stand for?

For loop for selecting date range with the requirement to meet certain criteria

SQL query. Should I loop through specific date range?

how do you through loop a certain date range

JavaScript loop between date range and return right format

Carbon loop through specific date range misses one day

Insert date range in a temp table without loop and cursor

Date range inside loop of multiple datatable in the same page

Loop through object and add all values that are in a date range

Speed up/replace the loop for millions data:judge multi date range

How to loop for date range within two tables in sql server

Django filter by date range and if no records in a date, return dummy records in that without being dead in loop

Check if the date is in date range

GROUP BY date range date

Search if a date in a range of date is include in a range of date

group date range with specific range

modify range in every loop of the range

How can i Loop TABLE TD with specific date and time range in PHP HTML

While loop in SQL Server 2008 iterating through a date-range and then INSERT

SQL for locating a date range using a date range

Select all where a range of date is in a range of date