1

My HTML Form has a

<input type="date">

Whereas my mysql database's table column is a date, which is stored in the format YYYY-MM-DD, however, the form submits the date field as DD-MM-YYYY, which MYSQL rejects.

Is there any way to get around this or to convert the date values into the correct format, or do i have to use another plugin to accomplish this? (e.g jquery ui)

userxxxso
  • 165
  • 2
  • 15

4 Answers4

1

In PHP

var $date = $your_input_date; //e.g. 03-01-2015
$date = date('Y-m-d', strottime($date)); 

http://php.net/manual/en/function.date.php

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

you can use javascript function to change date format

function reformatDate(dateStr)
{
  dArr = dateStr.split("-");  // ex input "2010-12-31"
  return dArr[2]+ "-" +dArr[1]+ "-" +dArr[0]; //ex out: "31-12-2010"
}

Check link for more details

Community
  • 1
  • 1
Himanshu
  • 4,327
  • 16
  • 31
  • 39
  • i see, is there a way whereby i can detect aLL date formats the user uses and convert them to YYYY-MM-DD? but pertaining to this question, your answer is correct. – userxxxso Jan 03 '15 at 06:46
0

You will need to insert the date in the database field in the format Y-m-d H:i:s. Hence on receiving the date from the form and parse it using the date function something like this: date('Y-m-d H:i:s', strtotime($date)) //where $date is the date field from the form

Aditya
  • 1,241
  • 5
  • 19
  • 29
0

MySQL has a function STR_TO_DATE, so you can convert at once in the query:

INSERT INTO atable (datetimefield)
VALUES (STR_TO_DATE('03-01-2015', '%d-%m-%Y'))

Mysql Documentation

Or you can use JQuery UI datepicker which has an option for an alternative field, where you can store the date in the correct format.

Gervs
  • 1,397
  • 9
  • 8