dscojuxf69080
2017-01-03 13:15
浏览 497
已采纳

使用Laravel的MySQL时区

I want to store a Carbon date time object in a MySQL Database and I am using Laravel Eloquent to do this.

I have build a test with different data types (dateTime, dateTimeTz, timestamp and timestampTz from Laravel Migrations) and every one cuts off the time zone and only stores the date and time. When the data is accessed again Laravel assumes the default time zone from config and I have invalid data.

Is there a way to either preserve the time zone or to automatically convert the date and time to UTC or the config time zone?

图片转代码服务由CSDN问答提供 功能建议

我想在MySQL数据库中存储 Carbon 日期时间对象,我正在使用 Laravel Eloquent 这样做。

我构建了一个具有不同数据类型的测试( dateTime dateTimeTz timestamp timestampTz ,每个人都会切断时区,只存储日期和时间。 再次访问数据时,Laravel采用 config 的默认时区,并且我的数据无效。

有没有办法保留时区或自动将日期和时间转换为UTC或配置时区?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duan00529 2017-01-03 18:36
    已采纳

    MySQL doesn't provide any features to store Timestamp with Timezone. You have to separately maintain all the timezone manually.

    You can define seprate column for maintaining the timezone when the Timestamp retrieved from the Database you can convert Timestamp to related Timezone.

    For that you can use Laravel features Accessors.

    点赞 评论

相关推荐 更多相似问题