Timezone issues with Symfony2 / Twig

I'm trying to figure out a weird timezone issue when displaying a datetime with Twig in a Symfony2 app and asking a couple of questions about what I see in one of my Twig templates when trying to display dates.

First, all I checked for misconfiguration:

  • My object classes have private properties defined as datetimes and public getter methods that return private property.
  • MySQL has dates stored in my timezone (America / New_York);
  • My php.ini file has a default timezone for America / New_York.
  • Nowhere do I call the twig method setTimezone()

    .
  • date_default_timezone_get()

    returns 'America / New_York', both from the web and from the CLI.

Now for the strangeness

In MySQL, my entity looked at data 2015-07-08 11:16:00

.

In one of my Twig templates, I call:

  • {{ entity.reviewedAt|date('m/d/Y h:i a') }}

    I am getting 07/08/2015 @ 05:16 pm .
  • {{ entity.reviewedAt|date('m/d/Y h:i a', false) }}

    I get 07/08/2015 @ 11:16 am .

The argument false

tells Twig to use the timezone of the datetime object passed to the date filter .

When I debug {{ dump(entity.reviewedAt) }}

I get:

object(DateTime)[879]
  public 'date' => string '2015-07-08 11:16:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/New_York' (length=16)

      

var_dump($entity-getReviewedAt())

from the controller returns the same result as {{ dump(entity.reviewedAt) }}

;

And this only happens on one template. Other templates on my site display this same field using a filter date()

, no problem.

First question:

Why, when the datetime object already has the same timezone as the default set in php.ini, do I need to pass an argument false

?

Even stranger:

My entity is a child instance

that has releaseDate

. In MySQL, this releaseDate is stored as 2015-07-02 00:00:00

.

When I display the release date in the same pattern since {{ entity.instance.releaseDate|date('m/d/Y h:i a') }}

I get 07/02/2015 @ 12: 00 am .

So this property is displayed correctly without being passed false

as the second argument. But when I do {{ dump(entity.instance.releaseDate) }}

I get:

object(DateTime)[5518]
  public 'date' => string '2015-07-02 00:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'Europe/Copenhagen' (length=17)

      

I'm sure Copenhagen is a wonderful city, but why the hell is it my time set on this one?

And if I call var_dump($entity->getInstance()->getReleaseDate())

from my controller I get:

object(DateTime)[1592]
  public 'date' => string '2015-07-02 00:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/New_York' (length=16)

      

And only when I call var_dump()

from the controller it {{ entity.instance.releaseDate|date('m/d/Y h:i a') }}

displays 07/02/2015 @ 06: 00 am and {{ dump(entity.instance.releaseDate) }}

displays:

object(DateTime)[1592]
  public 'date' => string '2015-07-02 00:00:00.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'America/New_York' (length=16)

      

Second question:

What .. Heck? Why does Twig show the timezone as Europe / Copenhagen, and why does it return to America / New_York when I call var_dump()

from the controller?

Could this be related to Doctrine2 proxy classes? When I {{ dump(entity.instance) }}

, I see that this instance is a Doctrine proxy object.

+3


source to share





All Articles