Symfony form not returning date range data

I have wrote a form in my Symfony project so it can return all results for given data range. I think problem is within the form as my query builder method works as it should.

When I input from 10.1 to 10.7 it returns till 10.6 not an input from last day (23:59:59).

    $start = DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 00:00:00") );
    $end = DateTime::createFromFormat( "Y-m-d H:i:s", date("Y-m-d 23:59:59") );

    $form = $this->createFormBuilder()
        ->add('startDate', DateTimeType::class, [
            'data' => $start,
            'widget' => 'single_text',
            'html5' => false,
            'attr' => [
                'class' => 'text-center',
            ],
            'format' => 'yyyy-MM-dd HH:mm',
            'label' => 'Start Date',
            'required' => true,
        ])
        ->add('endDate', DateTimeType::class, [
            'data' => $end,
            'widget' => 'single_text',
            'html5' => false,
            'attr' => [
                'class' => 'text-center',
            ],
            'format' => 'yyyy-MM-dd HH:mm',
            'label' => 'End Date',
            'required' => true,
        ])
        ->add(
            'save',
            SubmitType::class,
            [
                'label' => 'Submit',
                'attr' => ['class' => 'btn-submit']
            ]
        )
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $data = $form->getData();
        $startDate = $data['startDate'];
        $endDate = $data['endDate'];

        return $this->redirectToRoute('report', [
            'startDate' => $startDate->format("Y-m-d"),
            'endDate' => $endDate->format("Y-m-d")
        ]);
    }

    return $this->render('details.html.twig', [
          'form' => $form->createView()
        ]);

I tought the problem is with

->format("Y-m-d")

but when I remove this it throws:

expects parameter 1 to be string, object given

I except all date that is entered to give all results in from 2020-10-01T00:00:00 to 2020-10-07T23:59:59

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *