I am using this Bootstrap DatePicker: code below
<input class="datepicker" name="date">
<script> //date picker js
$(document).ready(function() {
$('.datepicker').datepicker({
todayHighlight: true,
"autoclose": true,
});
});
</script>
and I capture that in my PHP here:
$date = $_POST['date'];
The problem is that the DatePicker gives me the format dd/mm/yyyy when I need it yyyy-mm-dd in my $date variable. How do I reformat this?
I'm sure that you can set this in the date picker, but in PHP you can use:
$date = DateTime::createFromFormat("d-m-Y", $_POST['date'])->format('Y-m-d');
And from Bootstrap DatePicker documentation: http://bootstrap-datepicker.readthedocs.org/en/latest/options.html#format
You can easily adapt https://stackoverflow.com/a/2487938/747609 to suit your need. Something along this line should solve your problem:
$originalDate = $_POST['date'];
$newDate = date("Y-m-d", strtotime($originalDate));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With