开发者

jQuery UI's "datepicker" modules date cant be saved to MySQL

开发者 https://www.devze.com 2023-02-23 05:17 出处:网络
I have the format of the date returned from Datepicker as (yy-mm-dd) which returns the string as 2011-04-22.

I have the format of the date returned from Datepicker as (yy-mm-dd) which returns the string as 2011-04-22.

When I want to INSERT it in the MySQL table in the column with datatype as DATE, I get 0000-开发者_JAVA百科00-00 saved. Browsing through SO, I found suggestions about FROM_UNIXTIME, but this gives me the date as 1970-01-01.

Any help ?


You can try with dateFormat option in datepicker

$('#date-picker').datepicker({
    dateFormat : 'yy-mm-dd'
});

If you want to give custom formats in view, you may need to convert the date to mysql format before inserting to the database. `

$insertDate  = date("Y-m-d",strtotime($date))` 


You need to verify that the datatype is setup as a MySQL DATE field (as opposed to a DATETIME field which requires the time formatted like "yyyy-mm-dd hh:mm:ss")

Also, the MySQL DATE datatype is looking for a 4-digit year field where you have a 2 digit field, so make sure you have Date Picker output as yyyy-mm-dd.

$.datepicker.formatDate('yyyy-mm-dd');

Also, If you are trying to get this going with unix timestamps, don't forget that Unix Timestamps in Javascript include milliseconds where in PHP/MySQL they are truncated to seconds. So if you do use FROM_UNIXTIME(time) in MySQL, make sure you divide the timestamp by 1000.


The Jquery UI datepicker also has a native parse method you can use to change a string into a date. The string needs to be formatted correctly already for this to work.

$.datepicker.parseDate( "yy-mm-dd", "2007-01-26" );


There is an even simpler way download the file jquery-ui.js from

http://code.jquery.com/ui/1.10.4/jquery-ui.js

go to line 3041

dateFormat: "mm/dd/yy",

Change it to your desired result i.e. yy/mm/dd and its done

dateFormat: "yy/mm/dd/",
0

精彩评论

暂无评论...
验证码 换一张
取 消