Laravel 5 时间戳未更新正确时间

Laravel 5 timestamp not update right time

将记录插入数据库后,created_at 时间戳不显示正确的时间,它从服务器晚了 2 小时....另外如果我输入 mysql SELECT NOW() 它显示正确的时间,知道有什么问题吗?

编辑..

日期取自 Carbon class...知道如何更改它吗?

laravel 的默认 timezoneUTC,它位于 config/app.php 文件中。

如果您想将 timezone 更改为您喜欢的 timezone,请从 this list or from this list 中选择您喜欢的 timezone 并将 UTC 替换为您选择的时区。

一些注意事项。根据 comments here,准确地说,最后 3 条评论:您 不应该 更改默认值。

Storing dates of different timezones in data source of a same application (by changing the timezone in config for current user & letting Laravel handle it from there on) is just asking for trouble & is a bad design. You will lose data integrity. What will happen when a user changes timezone? You'll update all the dates in the database for that user?

Store dates for all users as UTC (or any other timezone of your choosing, just select one & stick to it). Laravel already uses the excellent Carbon library, use it to convert dates from your timezone (in which they're stored in DB) to users' timezone (which you would store with in every user's settings) when you display the dates.

对于其他人,在更改配置文件后仍然有错误date/time:

我的问题出在我的 vagrant box (Homestead) 中 php。

为了解决这个问题我做了:

首先 ssh 进入你的 vagrant box,然后 运行 在命令行中这样做:

date

这应该 return 类似于 "Mon Jul 3 13:48:52 CEST 2017"。检查这个 date/time 是否正确。如果没有,请执行以下操作:

sudo ntpdate -b pool.ntp.org

这应该会更新您的系统时间。使用第一个命令再次检查它。如果它仍然没有写入,您可能必须更改系统时区。您可以通过 运行ning:

sudo rm -f /etc/localtime

sudo ln -sf /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime 

您可以使用任何您喜欢的时区,我更喜欢 "Europe/Amsterdam"。

您可以获得其他时区here