dongwenyou4298 2018-09-25 19:32
浏览 259
已采纳

Carbon将ISO 8601字符串解析为UTC日期并将其记录到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?

  • 写回答

1条回答 默认 最新

  • dongsong8932 2018-09-25 20:35
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?