skip to Main Content

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…

2

Answers


  1. 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-dTH: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

    Login or Signup to reply.
  2. The now() method in Laravel returns a Carbon instance, which allows you to set the timezone using the tz() method. However, when you use dd(), it displays the date and time in the timezone you set. But when you return the result, it uses the default timezone of your application, which is set in the config/app.php file.

    To make sure that the timezone is correct when you return the result, you can use the tz() method to set the timezone before returning it. For example:

    $date = now('America/Los_Angeles')->tz('America/Los_Angeles');
    return $date;
    

    This will set the timezone to America/Los_Angeles before returning the date, so it should display correctly in the browser.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search