Carbon parse Iso 8601 string to UTC date and record it to db
Asked Answered
S

2

14

I have the following Iso8601 date-time string 2018-03-12T10:34:15-0200 and after I parse it

Carbon::parse("2018-03-21T10:34:15-0200", 'UTC')

and save it to mysql db datetime column I have 2018-03-21 10:34:15 so I've lost the -0200 hours difference with UTC timezone.

Any ideas how to solve it the right way?

Simard answered 25/9, 2018 at 19:32 Comment(0)
E
29

You don't need to pass time zone as a second parameter to parse function. Time zone is already part of date string. If you need to save date in UTC just convert it to UTC timezone after parsing like so:

Carbon::parse("2018-03-21T10:34:15-0200")->setTimezone('UTC')

Converted date will be: 2018-03-21 12:34:15.0 UTC (+00:00)

Etude answered 25/9, 2018 at 20:35 Comment(1)
Thank you very muchSimard
L
3

You can try something like this:

Carbon::parse("2021-12-23T07:42:42.393Z")->setTimezone('UTC')->format('Y-m-d H:i:s');

And the result will be like 2021-12-23 07:42:42.

Laborer answered 23/12, 2021 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.