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?
****** SOLUTION ******
To solve this issue I saw that the variable 'date' was formatted dd/mm/yyyy and was a string. I was able to parse it and rebuild as a date in the proper format.
list($m,$d,$y) = explode("/",$_POST['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$date = date("Y-m-d",$timestamp);
*Note - I also had to surround the $date variable in single quotes in the query:
$sql = "INSERT INTO tableName (..., date, ...)
VALUES (..., '".$date."', ...)";
Try this page http://php.net/manual/en/datetime.createfromformat.php. Convert the string to a date object and output it as a formatted date
You want Y-m-d.
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));