Search code examples
phpsymfony4fosrestbundle

Timezone is not changing in the routed function


I have this symfony4.3 (latest) PHP application and I am trying to insert to the database using Doctrine ORM and I need to insert the time which this query happened but I need a specific timezone and the timezone is not changing using either the date_default_timezone_set() or setTimeZone()

$entityManager = $this->getDoctrine()->getManager();
date_default_timezone_set("America/Bahia");
$customer = new Customer();
$customer->setName($name);
$customer->setPhone($phone);
$dt = new \DateTimeImmutable(date('m/d/y G:i:s'));
$tz = new \DateTimeZone("America/Bahia");
$dt->setTimezone($tz);
$customer->setCreated($dt);

I expect the timezone in the created cell to be changed to America/Bahia

Actual Output : Europe/Helsinki which is the timezone of the server


Solution

  • So it turns out it was changing the timezone correctly and storing the right value in the database, But the problem was that everytime you try to access the timezone from the datetime object after getting it from the database it will only show the timezone of the php.ini file or something and not the timezone you actually used to store the datetime with!. I think if we actually need the exact timezone used during insert or update we need to store it in a different column.