如何在zf2中将日期时间从mysql转换为doctrine2 odm



我在zf2中使用了doctrine2 Odm,但我的更新查询存储了错误的日期结果我如何在doctrine2-Odm中设置日期格式?这是我的更新查询:

if ($this->getRequest()->isPost()) {
                $post = $this->getRequest()->getPost();
                $form->setInputFilter($form->getInputFilter());
                $form->setData($post);
                //echo '<pre>';
                //print_r($post);
                //die();
                if($form->isValid())
                {
                    $formData=$form->getData();
                    $update=$dm->createQueryBuilder('CalendarDocumentEvent')
                    ->update()
                    ->field('title')->set($post['title'])
                    ->field('description')->set($post['description'])
                    ->field('begin')->set(date('Y-m-d H:i:s', $post['begin']))
                    ->field('end')->set(date('Y-m-d H:i:s', $post['end']))
                    ->field('id')->equals($id)
                    ->getQuery()
                    ->execute();
                    return $this->redirect()->toRoute('calendar');
                    //return $this->redirect()->toRoute('calendar', array('action' => 'show', 'id' => $form->get('calendar_id')->getValue()));
                }

但它存储begin=1970-01-01 05:33:34和end=1970-01:01 05:3:33:34,我如何存储正确的日期格式,如2014年6月19日上午6:33?

您需要使用

->field('begin')->set(new DateTime($post['begin']))

条令将以正确的方式格式化数据。

最新更新