Closed
Description
Hi, I'm having this issue in one of three field with date format.
Entity:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* DatePrice
*
* @ORM\Entity
* @ORM\Table(name="date_price")
*/
class DatePrice {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="date")
*/
private $date1;
/**
* @ORM\Column(type="date")
*/
private $date2;
/**
* @ORM\Column(type="date")
*/
private $date3;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getDate1()
{
return $this->date1;
}
public function setDate1($date1)
{
$this->date1 = $date1;
return $this;
}
public function getDate2()
{
return $this->date2;
}
public function setDate2($date2)
{
$this->date2 = $date2;
return $this;
}
public function getDate3()
{
return $this->date3;
}
public function setDate3($date3)
{
$this->date3 = $date3;
return $this;
}
}`
DatePriceType:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilderInterface,
Symfony\Component\OptionsResolver\OptionsResolverInterface;
class DatePriceType extends AbstractType
{
const NAME = 'appBundleDatePriceType';
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date1', 'date',array(
'required' => FALSE,
'widget' => 'single_text',
'attr' => array('class' => 'form-control'),
'format' => $options['date_format'],
'html5' => FALSE,
))
->add('date2', 'date',array(
'required' => FALSE,
'widget' => 'single_text',
'attr' => array('class' => 'form-control'),
'format' => $options['date_format'],
'html5' => FALSE,
))
->add('date3', 'date',array(
'required' => FALSE,
'widget' => 'single_text',
'attr' => array('class' => 'form-control'),
'format' => $options['date_format'],
'html5' => FALSE,
))
->add('save', 'submit', array(
'translation_domain' => 'price',
'label' => 'date_index.form.btn_save',
'attr' => array(
'class' => 'btn btn-success',
),
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\DatePrice',
'date_format' => 'dd/MM/yyyy',
));
}
public function getName()
{
return self::NAME;
}
}
PriceController:
/**
* @Route("/date", name="admin_price_date", options={"expose" = true})
*/
public function dateIndex(Request $request)
{
$datePrice = $this->getDoctrine()->getRepository('AppBundle:DatePrice')->find(1);
switch ($request->getLocale()) {
default:
$dateFormat = 'dd/MM/yyyy';
}
$form = $this->createForm(new DatePriceType(), $datePrice , array(
'date_format' => $dateFormat,
));
$form->handleRequest($request);
if ($form->isValid()) {
$datePriceService = $this->get(DatePriceService::DATE_PRICE_SERVICE);
// Hack to bug with date that shift one day
$datePrice->setDate3($datePrice->getDate3()->add(new \DateInterval('PT1H')));
$datePriceService->update($datePrice);
$this->clearCache();
return Util::jsonResponse(array());
}
return $this->render('AppBundle:Admin/Price:dateIndex.html.twig', array(
'form' => $form->createView(),
));
}
I have to add the line with $datePrice->setDate3($datePrice->getDate3()->add(new \DateInterval('PT1H')));
because in that field after handleRequest date was shifted in one hour
The most strange is because it only happend on date3, on date1 or date2 works fine.
my phi.ini
date.timezone = America/Montevideo
FYI: Uruguay always use day light saving, but this year don't apply. so we are now -3GMT.
In form I complete for ex: 25/10/2015
and after handleRequest I can see the object is 24/10/2015 23:00:00. It shif one hour!!
I found this problem searching the web, and seems to be a solved problem. but I'm having now this.
Thanks
Pablo Minetti