1
votes

I m using following code.

static::$timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC');

'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone)->setTimezone(static::$timezone),

but I m getting error.

Fatal error: Call to a member function setTimezone() on a non-object

So what is wrong in the code

1
->setTimezone is not needed, just make sure static::$timezone is indeed a DateTimezone objectKevin

1 Answers

1
votes

No need to use ->setTimeZone there. Just make sure static::$timezone is indeed a DateTimeZone object since you already fed it with that:

static::$timezone = new DateTimeZone('America/Los_Angeles');

Then on creating the DateTime object:

'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), static::$timezone)