MySQL日期格式 [英] MySQL Date Format

查看:96
本文介绍了MySQL日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML表单上有一个字段,要求用户输入日期为dd / mm / yyyy。然后我运行一个将数据保存到mySQL数据库的PHP脚本,或者如果我可以使用日期格式整理出一个问题。

I have a field on my HTML form which asks the user to type the date as dd/mm/yyyy. I then run a PHP script that saves the data to a mySQL database, or it would if I could sort out an issue I have with the date format.

我知道默认情况下,mySQL数据库将日期保存为yyyy-mm-dd,但有人可能会告诉我,如何继续保持用户输入的日期格式为dd / mm / yyyy,但是当它遇到数据库时才正确转换为mySQL日期格式。

I know that by default the mySQL database saves dates as yyyy-mm-dd, but could someone perhaps tell me please how I can continue to keep the user input date format as dd/mm/yyyy but then when it hits the database it is correctly converted to the mySQL date format.

推荐答案

mysql的格式确实是YYYY-MM-DD

我建议如下(未经测试):

I suggest something like the following (untested):

$userInput = '24/12/2004';
date('c',  date_parse($userInput));

要做的事情是使用准备好的语句并将日期直接发送到准备好的语句。唉, Php-MySQL准备语句不允许使用日期类型

The sane thing to do would be to use prepared statements and send the date directly to the prepared statement. Alas, Php-MySQL prepared statements don't allow for a date type.

这篇关于MySQL日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆