Search code examples
laravelphp-carbon

Laravel `now('America/Los_Angeles')` is correct on `dd()` but on return result is in UTC


For me it's strange, i set 'timezone' => 'America/Los_Angeles',

cleared config & cache.

but now when i dd(now()) or dd(now('America/Los_Angeles')) i get

enter image description here

and when return an array like below, result aren't in desired time zone.

enter image description here

i want default now() return result in desired time zone...


Solution

  • This is the expected behavior, what you see is how it's converted when you ask for JSON.

    You can explicitly format it with ->format('Y-m-d\TH:i:s.uP T') or whatever other format or you can change globally how it's converted to string when a Carbon object is returned in a JSON response.

    See: https://carbon.nesbot.com/docs/#api-json https://github.com/laravel/ideas/issues/1940