Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

k-web-dev's avatar

When passing a Carbon date time with json I get a different timezone

In my config/app I have set up the time zone:

'timezone' => 'Europe/Sofia',

I have a variable $workingHours with two date objects in it.

All is working fine in the backend. If I dd($workingHours) I get the two objects with in the correct timezone.

2020-04-20 09:31:00.0 Europe/Sofia (+03:00)
2020-04-20 17:01:00.0 Europe/Sofia (+03:00)

As soon as I return a json:

return response()->json([$workingHours]);

Insomnia HTTP gets them with a 3 hour delay:

 "shopWorkingHours": {
    "openingHours": "2020-04-20T06:31:00.000000Z",
    "closingHours": "2020-04-20T14:01:00.000000Z"
  } 

I have tried to set up the timezone to 'GMT+3' - still no result.

I have also tried to set the timezone on the Carbon objects individually with the ->setTimezone() method - still no result.

As far as I can tell the HTTP client - Insomnia does not have a way to change timezone - it uses the local.

The only thing that work is if I use the ->toDateTimeString() which returns them as strings, but it will require to convert them back into date time object with momen.js.

Is there a way to pass them in the correct time zone ?

0 likes
2 replies
k-web-dev's avatar

This seems terrible !!! Thank for the help :)

1 like

Please or to participate in this conversation.