Chathan
Chathan

Reputation: 65

How convert and save date as mysql format?

Have a form and its consist of 10 date fields, and user can pick upto 10 dates. Picking in the format 'D, d M, Y'. How can i convert this format to mysql date format and save into database....

<input type='text' name='day1' id='datepicker1' value='' maxlength="50" style="color:#999999" readonly="readonly"/>
<input type='text' name='day2' id='datepicker2' value='' maxlength="50" style="color:#999999" readonly="readonly"/>

Upvotes: 3

Views: 5976

Answers (3)

smac2020
smac2020

Reputation: 10704

Simple in Java - you can convert a value obtained from an HTML Date widget like this:

//Assume HTML widget returns 2015-05-25 
DateFormat format = new SimpleDateFormat("yyyy-mm-dd");
Date fromDate1 = format.parse(fromDate);

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146350

Tell MySQL about your format in the STR_TO_DATE() function.

Upvotes: 0

Francois Deschenes
Francois Deschenes

Reputation: 24969

If you use strtotime, you can convert the date into a timestamp, and reformat it for MySQL using date.

Example:

$day1 = strtotime($_REQUEST['day1']);
$day1sql = date('Y-m-d H:i:s', $day1);

You then want to use $day1sql to insert into MySQL. Repeat for your other dates. For more information, have a look at this.

Upvotes: 6

Related Questions