This question already has an answer here:
$('.date-own').datepicker({
minViewMode: 1,
format: 'yyyy/mm/dd',
autoclose: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
<body>
<form>
<input class="date-own form-control" style="width: 300px;" type="text">
<input type="submit" value="submit" name="submit" />
</form>
</body>
I want to use those variables there as a month and year for SQL.
$query = "SELECT `distributor`.`distributor_name`, `orders`.*, `products`.*
FROM `products`
LEFT JOIN `orders` ON `orders`.`pro_id` = `products`.`pro_ID`
LEFT JOIN `distributor` ON `orders`.`d_id` = `distributor`.`d_ID`
WHERE MONTH(orders.order_date) = 10 AND YEAR(orders.order_date) = 2017";
</div>
First, you'll have to add a name
attribute to your <input>
for the datepicker:
<input class="date-own form-control" style="width: 300px;" type="text" name="date"/>
Next, have PHP accept the date, and convert it to a DateTime using createfromformat
, with the format you specified for the javascript datepicker (yyyy/mm/dd). You can then use DateTime's format
method to output the month and year.
// Get the input into a date object
$input_date = DateTime::createFromFormat('Y/m/j', $_POST['date']);
// Output the month
print $input_date->format('m');
// Output the year
print $input_date->format('Y')