user4488093
user4488093

Reputation:

Why Symfony2 DateTime returns a day earlier?

My code in controller:

  $yesterday = new \DateTime();
  $yesterday->sub(new \DateInterval('P1D'));
  $customerReportForm->add('dateFrom', 'date', ['data'=> $yesterday 'attr' => array('class' => 'datepicker',  "data-date-format"=>"yy-mm-dd")]);
  $customerReportForm->add('dateTo', 'date', ['data'=>$yesterday,' 'attr' => array('class' => 'datepicker',  "data-date-format"=>"yy-mm-dd")]);

And I have POST handler in same controller method:

if($this->getRequest()->isMethod('POST'))
{
  $dateFrom = $customerReportForm->get('dateFrom')->getData();
  $dateTo = $customerReportForm->get('dateTo')->getData();
}

values of dateFrom and dateTo shows me date a day earlier then I'm select in form.

For example, if I'm will type in datepicker "2015-30-12" it will show me "2015-29-12"

Why?

Just var_dump(new DateTime()) shows correct date.

PHP 5.5.29 Symfony - 2.3

Upvotes: 2

Views: 131

Answers (1)

miltone
miltone

Reputation: 4721

I don't understand what is your real problem... You use $yesterday->sub(new \DateInterval('P1D'));. It's normally behaviour that your date is earlier than today.

You use a sub function php on the new DateTime.

Upvotes: 1

Related Questions