Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 623
Bat Man Avatar asked Dec 01 '22 10:12

Bat Man


1 Answers

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)

like image 131
Vedmant Avatar answered Jan 30 '23 14:01

Vedmant