开发者

formatting date for insertion into mysql date

I am using jQueryUI datepicker and making sure that the date, mm/dd/yyyy is shown to the user so they understand what they entered is correct. My only problem is I don't understand how to then take that date and format it to go into mysql date 开发者_开发技巧field correctly?


That's pretty easy if you know a little PHP.
Because PHP being a string manipulation language, it is a must to know some string manipulation basics.

You can use strpos() function to locate position of / symbol and then use substr() to get numbers and then use . concatenation operator to get date parts in order

You can use very handy explode() function, to get date parts into array and then combine them using some variable interpolation, like this:

$d          = explode("/",$date);
$date_mysql = "$d[2]-$d[0]-$d[1]";

When mastered your string manipulation skills, you can start with real powerful voodoo of regular expressions.


You can use STR_TO_DATE function in mysql.

$date is the variable that came from Jquery via Ajax, holding the date that user picked up.

Insert into tableName Values('bla', 'bla', STR_TO_DATE($date))


Convert your MM/DD/YYYY string to ISO 8601 format, YYYY-MM-DD, and then pretty much any database (including MySQL) will interpret your date properly.


PHP:

$date = date('Y-m-d', strtotime($old_date_format));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜