I’m trying to create several database entries with a start date and an end date.
The whole thing should always be created in a 7-day period. For example: start date: June 6th, 2024; end date: June 18th, 2024. In this case, database entries should be created for June 6th, June 12th and June 18th. Within the foreach loop, up to 8 entries should be made for each individual date (hour 1 – 8). The whole thing becomes a calendar. Someone enters an appointment, this appointment can go from the first hour to the 8th hour. And this appointment should be entered weekly (every 7 days) up to an end date. Unfortunately, I can’t see the forest for the trees at the moment. This is what my code looks like:
$startDateTime = new DateTime();
$endDateTime = new DateTime();
$dateInterval = new DateInterval( 'P7D' );
$datePeriod = new DatePeriod($startDateTime->setTimestamp($fromform->date), $dateInterval, $endDateTime->setTimestamp($fromform->reservto));
foreach ( $datePeriod as $dateTime ) {
//Here the individual hours (1-8) are counted up.
for ( $i = $fromform->time; $i = $fromform->timeto; $i++ ) {
//I want to save this format in the database.
$newdate = $dateTime->format("Y-m-d");
$recordtoinsert = new stdClass();
$recordtoinsert->datum = $newdate;
$recordtoinsert->ustd = $i;
$recordtoinsert->resfuer = $fromform->resfuer;
if ( $fromform->room == 1 ) {
$buchungfromform = 'Raum: ' . $fromform->raum . ' durch ' . $fromform->resfuer;
$bgcolor = 'blue';
} elseif ( $fromform->laptopcheck == 1 ) {
$buchungfromform = 'Laptop: ' . $fromform->laptop . '(' . $fromform->timelaptopanzahl . ' Stk.)' . ' durch ' . $fromform->resfuer;
$bgcolor = 'green';
}
$recordtoinsert->buchung = $buchungfromform;
$recordtoinsert->userid = $fromform->userid;
$recordtoinsert->bgcolor = $bgcolor;
$DB->insert_record( 'bs_steinweg', $recordtoinsert );
//Zurück zur Lernbüroseite
}
$getsaved = get_string( 'saved', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/bs_sw.php', $getsaved );
}
When I make the database entries, it creates tens of thousands of entries in a continuous loop.