且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

日期不保存在mysql数据库中

更新时间:2023-02-15 12:42:23

我想这是造成您问题的原因:

I would guess this is the cause of your problem:

$evtStartDate->addValidator(new Zend_Validate_Date('dd-mm-YYYY'));

MySQL无法理解格式为dd-mm-YYYY的日期文字. MySQL理解YYYY-MM-DDYY-MM-DD以及其他一些变体.

MySQL doesn't understand date literals in the format dd-mm-YYYY. MySQL understands YYYY-MM-DD or YY-MM-DD, and a few other variations.

请参见 http://dev.mysql.com/doc/refman/5.1/en/datetime.html 获取有关接受日期文字格式的官方文档.

See http://dev.mysql.com/doc/refman/5.1/en/datetime.html for official documentation on accepted date literal formats.

评论:您可以在PHP代码中转换日期,也可以使用

Re comment: You can either convert the date in your PHP code, or else you can insert an expressing using the STR_TO_DATE() MySQL function. Pass a Zend_Db_Expr object in place of the literal value.

$startdate_expr = $this->getAdapter()->quoteInto("STR_TO_DATE(?, '%d-%m-%Y')",
  $formdata["evt_startdate"]);
$formdata["evt_startdate"] = new Zend_Db_Expr($startdate_expr);

$enddate_expr = $this->getAdapter()->quoteInto("STR_TO_DATE(?, '%d-%m-%Y')",
  $formdata["evt_enddate"]);
$formdata["evt_enddate"] = new Zend_Db_Expr($enddate_expr);

$e->insert($formdata);

或者您可以更改Web应用程序的格式,以要求日期采用YYYY-MM-DD格式,这样就不必进行任何转换.

Or you can change your web application's form to require dates to be in YYYY-MM-DD format, and then you don't have to convert anything.