为什么Symfony2 DateTime提前一天返回?

时间:2022-06-16 07:17:37

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:

我在同一控制器方法中有POST处理程序:

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.

dateFrom和dateTo的值显示我前一天的日期然后我在表单中选择。

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

例如,如果我要输入datepicker“2015-30-12”,它会显示“2015-29-12”

Why?

为什么?

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

只需var_dump(new DateTime())显示正确的日期。

PHP 5.5.29 Symfony - 2.3

PHP 5.5.29 Symfony - 2.3

1 个解决方案

#1


1  

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.

我不明白你的真正问题是什么......你使用$ yesterday-> sub(new \ DateInterval('P1D'));.通常,您的约会时间早于今天。

You use a sub function php on the new DateTime.

您在新的DateTime上使用子函数php。

#1


1  

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.

我不明白你的真正问题是什么......你使用$ yesterday-> sub(new \ DateInterval('P1D'));.通常,您的约会时间早于今天。

You use a sub function php on the new DateTime.

您在新的DateTime上使用子函数php。