0

I am getting date input from bootstrap datepicker. My selected date is 10th September 2020

$("#startDatePicker").datepicker({
        format:  'mm/dd/yyyy'
    })

<input id="startDatePicker" placeholder="MM/DD/YYYY" name="from_date" type="text">

If the date format as mm/dd/yyyy means PHP return the correct date

$from = date("Y-M-d", strtotime($from_date));
output : 2020-Sep-10

$from = date("Y-m-d", strtotime($from_date));
output : 2020-09-10

I have changed the date format as dd\mm\yyyy in date picker.

$("#startDatePicker").datepicker({
        format:  'dd/mm/yyyy'
    })

<input id="startDatePicker" placeholder="DD/MM/YYYY" name="from_date" type="text">

In PHP return the in correct date

 $from = date("Y-M-d", strtotime($from_date));
 output : 2020-Oct-09
 
 $from = date("Y-m-d", strtotime($from_date));
 output : 2020-10-09

How can i get 2020-09-10 format in PHP from any date format in bootstrap date picker?.

Because i am facing problem when filter the data between the dates.

Thanks in advance!

Siva
  • 1,481
  • 1
  • 18
  • 29
  • you can use `DateTime` classes in PHP and explicitly tell that the format you're feeding is `mm/dd/yy` or whichever kind you'd want to use: https://www.php.net/manual/en/datetime.createfromformat.php – Kevin Sep 10 '20 at 15:31
  • 1
    `/` is always interpreted as `m/d/y` if you want `strtotime` to see day first then use `-` as `d-m-y`. – AbraCadaver Sep 10 '20 at 15:34
  • There are many variations of this already answered on SO, the TLDR is: instead of `strtotime`, use `DateTimeImmutable::createFromFormat` and provide the input format. Then you can do whatever you like by calling the [`format`](https://www.php.net/manual/en/datetime.format.php) method – Jon Sep 10 '20 at 15:35

0 Answers0