jona303
jona303

Reputation: 1568

Symfony time input and timezone

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']])

Upvotes: 1

Views: 1884

Answers (2)

Tsounabe
Tsounabe

Reputation: 2184

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.

Upvotes: 2

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27325

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

Upvotes: 1

Related Questions