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

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)

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

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题