php - Handle zulu datetime from json object in Laravel
I get from APIs different time attributes:
"created_at": "2017-08-18T18:54:02.30709Z",
"updated_at": "2021-05-25T10:20:49.833322Z",
"publishedAt": "2021-05-25T05:25:27Z",
I would like to save them in the database:
$table->timestamp('created_at');
$table->timestamp('updated_at');
$table->timestamp('published_at');
Unfortunately, they are saved with the wrong time. So 2021-05-25T05:25:27Z
is stored as 2021-05-25 05:25:27 GMT+2
in the database without offset.
What is the best way to use Laravel to transfer this data from an API to the database?
Answer
Solution:
funtion with
return \Illuminate\Support\Carbon::parse($string)->setTimezone(config('app.timezone'));
works.
Source