简体   繁体   中英

how to convert datetime from mysql to doctrine2 odm in zf2?

i used doctrine2 Odm in zf2 but my update query store wrong result of date how i set date format in doctrine2 Odm? here is my update query:

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('Calendar\Document\Event')
                    ->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()));

                }

but it stores begin=1970-01-01 05:33:34 and end=1970-01-01 05:33:34,how i store correct date format like 6/19/2014 6:33:00 AM?

You need to use

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

Doctrine will format the data the right way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM