时间戳与数据库不一样(Laravel)

发布于 2025-02-10 10:18:44 字数 569 浏览 1 评论 0原文

我有一些数据。在数据库mysql 8上。 Create_at字段看起来像2022-06-25 07:44:43。

但是当我获取数据时,它返回

“ create_at”:“ 2022-06-25T05:44:43.00000000Z”

我将 config/app.php 的时区设置为欧洲/法国,但时间似乎仍然落后2个小时。

在提取期间,哪种机制发生了变化?为什么我不从数据库中获取相同的数据?

下面的代码解决了我的问题,但我只是对这个问题感到好奇。

public function getCreatedAtAttribute($value) {
        return Carbon::createFromTimestamp(strtotime($value))
            ->timezone(env("APP_TIMEZONE"))
            ->toDateTimeString();
    }

I have some data. On the database mysql 8. Created_at field seems like 2022-06-25 07:44:43.

But when I fetch the data, it returns

"created_at": "2022-06-25T05:44:43.000000Z"

I have set my timezone from config/app.php as Europe/France but the time seems still 2 hours behind.

Which mechanism changes that time during the fetch? Why don't I get the same data from the database?

The code below solves my problem but I am just curious about this problem.

public function getCreatedAtAttribute($value) {
        return Carbon::createFromTimestamp(strtotime($value))
            ->timezone(env("APP_TIMEZONE"))
            ->toDateTimeString();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2025-02-17 10:18:44

错误设置了数据库服务器上的时区。

如果您在Linux运行

$ sudo timedatectl set-timezone Europe/Paris

或时区,则需要

The timezone on your database server is wrongly set.

If you are under linux run

$ sudo timedatectl set-timezone Europe/Paris

Or the timezone you need

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文