How to Format the Bootstrap DatePicker for PHP

12,410

Solution 1

****** 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."', ...)";

Solution 2

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

Solution 3

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));
Share:
12,410
Nate May
Author by

Nate May

5 years Army Infantry | Deployments (2): Baghdad, Kandahar University of Illinois | Majors (2): Accounting, Finance - Minors (2): Technology & Management, Informatics Deloitte Consulting | intern FTI Consulting | Data Consultant TATA Consulting | Web Developer (Angular, Jquery) CitiGroup | Development Team Lead (Angular) University of Michigan | MS Information - Minor Entrepreneurship

Updated on June 07, 2022

Comments

  • Nate May
    Nate May almost 2 years

    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?