After about a month I have the date-picker working, storing all dates in DB on their own row.
It picks a start date & end date from two datepickers.
Creates/produces all the dates in between then inserts all those separate dates into the database each date in it's own row in PHP.
Then redirects to same page & highlights all DB dates & disables them so no one can select already booked dates.
I have copied different bits of code from this site and manipulated it a little & at present I'm very happy with the hybrid outcome.
This is running on a WAMP server.
Below is my code to help other amateurs like me.
<?php
$servername = "localhost:3308";
$username = "root";
$password = "";
$dbname = "datepicker_test_outputs";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT date FROM insert_datesarray";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$for_JS_date = null;
// output data of each row
while($row = $result->fetch_assoc()) {
$php_array_dates = date('Y-m-d',strtotime($row["date"]));
$for_JS_date .= "'".$php_array_dates."'".",";
$correct_date_format = substr($for_JS_date, 0, -1);
}
if($_SERVER["REQUEST_METHOD"]=="POST"){
$start_date = $_POST["from"];
$end_date = $_POST["to"];
$dateentry = array();
// populate $dateentry array with dates
while (strtotime($start_date) <= strtotime($end_date)) {
$dateentry[] = date("Y-m-d", strtotime($start_date));
$start_date = date ("Y-m-d", strtotime("+1 day", strtotime($start_date)));
} // end while
// loop through $dateentry and insert each date into database
foreach($dateentry as $entry) {
$my_inserted_dates =("INSERT INTO insert_datesarray
(date) VALUES('{$entry}')")
or die(mysqli_error());
$result = mysqli_query($conn,$my_inserted_dates);
if($result == false)
{
echo "<script>alert('BOOKING IS NOT SUCCESS - PLEASE CONTACT ADMINISTRATOR'); </script>";
}
else
{
/* Header location to refresh the page & load booked dates */
header('Location: '.$_SERVER['PHP_SELF']);
}
} die;
// end foreach
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery UI DatePicker</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<style>
.ui-highlight .ui-state-default{
background: red !important;
border-color: red !important;
color: white !important;
cursor:not-allowed !important;
}
</style>
<script type="text/javascript" language="javascript">
var dates = [<?php echo $correct_date_format;?>];
/*var dates = ['2021-04-05','2021-04-15','2021-04-25','2021-04-30'];*/
jQuery(function(){
jQuery('input[id=from]').datepicker({
dateFormat: 'dd-mm-yy',/*CN Changes date format. Remove if required CN*/
changeMonth : true,
changeYear : true,
minDate: 0 ,/*Mindate disables dates before todays date*/
beforeShowDay : function(date){
var y = date.getFullYear().toString(); // get full year
var m = (date.getMonth() + 1).toString(); // get month.
var d = date.getDate().toString(); // get Day
if(m.length == 1){ m = '0' + m; } // append zero(0) if single digit
if(d.length == 1){ d = '0' + d; } // append zero(0) if single digit
var currDate = y+'-'+m+'-'+d;
if(dates.indexOf(currDate) >= 0){
return [true, "ui-highlight", 'Date Already Booked'];
}else{
return [true];
}
}
});
})
</script>
<script type="text/javascript" language="javascript">
var dates = [<?php echo $correct_date_format;?>];
/*var dates = ['2021-04-05','2021-04-15','2021-04-25','2021-04-30'];*/
jQuery(function(){
jQuery('input[id=to]').datepicker({
dateFormat: 'dd-mm-yy',/*CN Changes date format. Remove if required CN*/
changeMonth : true,
changeYear : true,
minDate: 0 ,/*Mindate disables dates before todays date*/
beforeShowDay : function(date){
var y = date.getFullYear().toString(); // get full year
var m = (date.getMonth() + 1).toString(); // get month.
var d = date.getDate().toString(); // get Day
if(m.length == 1){ m = '0' + m; } // append zero(0) if single digit
if(d.length == 1){ d = '0' + d; } // append zero(0) if single digit
var currDate = y+'-'+m+'-'+d;
if(dates.indexOf(currDate) >= 0){
return [true, "ui-highlight", 'Date Already Booked'];
}else{
return [true];
}
}
});
})
</script>
</head>
<body>
<p id="dateFrom"></p>
<p id="dateTo"></p>
<form action="" name="form1" method="post">
<label for="from">From</label>
<input type="text" id="from" name="from"onchange="getFromDate()">
<label for="to">to</label>
<input type="text" id="to" name="to"onchange="getToDate()">
<input name="book" type="submit" value="Book">
</form>
</body>
</html>