1

I have an Entity with a time property. In the form i have a TimeType. The time I write in the form is exactly the time stored in DB ex. 14:54. But when I get it back from my DB I got 13:54. I guess it's a timezone issue but I can't figure out how to deal with this. I tried setting model_timezone and view_timezone but it doesn't seem the change anything. My issue is wioth the startTime property

EDIT : Here's some code

Entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
 * BookingSession
 *
 * @ORM\Table(name="booking_session")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\BookingSessionRepository")
 */
class BookingSession
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Groups({"base"})
     */
    private $id;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="date", type="datetimetz")
 * @Groups({"base"})
 */
private $date;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="startTime", type="time")
 * @Groups({"base"})
 */
private $startTime;
//...

The form

$builder
    ->add('startTime', 'time',['widget' => 'single_text','hours'=>$hours,"label"=>false,"attr"=>['class'=>'hidden']])
jona303
  • 1,358
  • 1
  • 9
  • 27

2 Answers2

2

This is because the Datetime object related to the TimeType field is built using the default date 1970-01-01.

When the time is converted into the user timezone, the Datetime object is calculated with the daylight saving of the 1st January 1970.

At this date some timezones didn't have daylight saving in application which can not returning the expected time.

Tsounabe
  • 2,109
  • 1
  • 16
  • 25
1

The question is what do you mean with "time" property. If you save your date as DateTime what i prefer then you get a DateTime object back and you can work with.

/**
 * @ORM\Column(type="datetime", nullable=true)
 */
private $mydate;

To configure the timezone you should take a look at the server if the timezone and date settings are correct and you can set the timezone in your php.ini file.

https://secure.php.net/manual/en/datetime.configuration.php

And here is another post that can help your with some problems.

Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • What I miss is why it stores the exact time value I wrote in the form, and when i get the entity back from my Repository I get the value modified. – jona303 Feb 17 '17 at 10:08
  • what is if you use "datetime" and not "datetimetz" as type in your entity? – René Höhle Feb 17 '17 at 10:10
  • I get the exact same result. I wrote 12:14, it stores 12:14, $startTime->format('H:i') write 11:14 – jona303 Feb 17 '17 at 10:17
  • 1
    Then everwhere in your system your datetime is not correct. Try to chat all points your system and your php installation. make a `phpinfo` and check which timezone is set. – René Höhle Feb 17 '17 at 10:19
  • PHP and mysql are set to "Europe/Berlin". – jona303 Feb 17 '17 at 10:35
  • In some cases that settings aren't working. Try to set that options in your AppKernel like the example. – René Höhle Feb 17 '17 at 10:36