I have a JSON array being passed form PHP and it looks like this:
[{from: "2019,09,14", to: "2019,09,14"}]
and I need it to look like this:
[{from: [2019, 9, 14], to: [2019, 9, 17]}]
here is my code thus far:
function getPricingDateRanges() { var city = document.getElementById("pricingcityselect").value;
$.ajax({
url: 'getpricingdaterangepicker.php?city=' + city, // Change this to the uri of your file
method: 'GET', // Use the GET method
dataType: 'json', // Expect a JSON response
success: function(response) { // What will happen when the request succeeds
var darray = [{from:[2019,9,14], to:[2019,9,17]}];
console.log(response);
console.log(darray);
}
});
}
Added code from my PHP:
<?php
include 'dbconfig.php';
$city = ($_GET['city']);
$sql="SELECT start_date, end_date FROM date_ranges WHERE city='" . $city . "'";
$result = mysqli_query($conn,$sql);
// Create empty array.
$date_ranges = array();
// Add every row to array;
while($row = mysqli_fetch_array($result)) {
// Create an associative array to store the values in.
// This will later be a JavaScript Object.
$from = new DateTime($row['start_date']);
$ffrom = $from->format('Y,m,d');
$to = new DateTime($row['end_date']);
$fto = $from->format('Y,m,d');
array_push($date_ranges, array(
'from' => $ffrom,
'to' => $fto
));
}
// Send the $date_ranges as JSON.
$json = json_encode($date_ranges); // '[{"start": "2019-08-18", "end": "2019-08-19"}]'
echo $json;
?>