带有日期的对象以错误的日期和格式保存

时间:2017-04-07 14:42:05

标签: php symfony datepicker symfony-forms bootstrap-datepicker

我使用Symfony 3和bootstrap-datepicker保存日期。

如果我填写表格,我希望在这种情况下保存日期,04/25/2017。

我想要的数据包是: 04/25/2017。

相反,我在转储中得到了这个:

  

2017-01-25 00:04.000000

并在我的数据库中:

  

2017- 01 -25

转储结果:

enter image description here

数据库值:

enter image description here

PlayLogController:

    <?php

namespace AppBundle\Controller;

use AppBundle\Entity\PlayLog;
use AppBundle\Entity\Game;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

/**
 * Playlog controller.
 *
 * @Route("playlog")
 */
class PlayLogController extends Controller
{
    /**
     * Lists all playLog entities.
     *
     * @Route("/", name="playlog_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $playLogs = $em->getRepository('AppBundle:PlayLog')->findAll();

        return $this->render('playlog/index.html.twig', array(
            'playLogs' => $playLogs,
        ));
    }
    /**
     * Creates a new playLog entity.
     *
     * @Route("/{gameId}/new", name="playlog_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request, $gameId)
    {
        $playlog = new PlayLog();
        $em = $this->getDoctrine()->getManager();
        $game = $em ->getRepository(Game::class)->find($gameId);
        $playlog->setGame($game);
        $form = $this->createForm('AppBundle\Form\PlayLogType', $playlog);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            /* @var $playLog PlayLog */
            $playlog = $form->getData();
           // echo $playlog->getGame()->getId() .'!';

            $em->persist($playlog);
            $em->flush();
        }

        return $this->render('playlog/new.html.twig', array(
            'playLog' => $playlog,
            'form' => $form->createView(),
        ));
    }
    /**
     * Finds and displays a playLog entity.
     *
     * @Route("/{id}", name="playlog_show")
     * @Method("GET")
     */
    public function showAction(PlayLog $playLog)
    {
        $deleteForm = $this->createDeleteForm($playLog);
        return $this->render('playlog/show.html.twig', array(
            'playLog' => $playLog,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing playLog entity.
     *
     * @Route("/{id}/edit", name="playlog_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, PlayLog $playLog)
    {
        $deleteForm = $this->createDeleteForm($playLog);
        $editForm = $this->createForm('AppBundle\Form\PlayLogType', $playLog);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('playlog_edit', array('id' => $playLog->getId()));
        }

        return $this->render('playlog/edit.html.twig', array(
            'playLog' => $playLog,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a playLog entity.
     *
     * @Route("/{id}", name="playlog_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, PlayLog $playLog)
    {
        $form = $this->createDeleteForm($playLog);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($playLog);
            $em->flush();
        }

        return $this->redirectToRoute('playlog_index');
    }

    /**
     * Creates a form to delete a playLog entity.
     *
     * @param PlayLog $playLog The playLog entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(PlayLog $playLog)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('playlog_delete', array('id' => $playLog->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}

PlayLogType:

<?php

namespace AppBundle\Form;

use AppBundle\Entity\PlayLog;
use Symfony\Component\Form\Extension\Core\Type\DateType;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PlayLogType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('date', DateType::class, array(
            'widget' => 'single_text',
            'html5' => false,
            'attr' => ['class' => 'js-datepicker'],
            'format' => 'mm/dd/yyyy'
            )
        );
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => PlayLog::class
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_playlog';
    }


}

我用于datepicker的脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $('.js-datepicker').datepicker({
            format: 'mm/dd/yyyy'
        });

    });
</script>

2 个答案:

答案 0 :(得分:4)

格式必须为'MM/dd/yyyy'mm是分钟,这就是您的记录中时间为00:04的原因。

答案 1 :(得分:0)

您发送到服务器的日期/时间格式将取决于服务器所在的区域设置。由于我在上面的屏幕截图中看到了Europe / Berlin,这意味着它希望日期/时间到以d / m / Y(php格式)进来。

另外,消除所有日期/时间格式的最佳方法是以YYYY-MM-DD H:i:s格式发送(如果你不关心,H:i:s可以省略)时间),或作为unix时间戳,保证在UTC时间..