Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Format the Bootstrap DatePicker for PHP

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?

like image 726
Nate May Avatar asked May 02 '26 19:05

Nate May


2 Answers

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

like image 144
Ron Dadon Avatar answered May 05 '26 08:05

Ron Dadon


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));
like image 39
IROEGBU Avatar answered May 05 '26 10:05

IROEGBU