PHP/Laravel-Carbon和DateTime返回错误的日期/时间



我有以下问题。我正在变量中保存Carbon或DateTime对象。

$now = Carbon::now('Europe/Berlin');

如果我是第一次使用此命令。一切都很好。但在一天后,它返回前一天生成的最后一个日期和时间。

例如:

此时,正确的日期和时间是:2018-12-14 13.48:00,但

dd($now);

返回

Carbon @1544733487 {#555 ▼
date: 2018-12-13 21:38:07.319843 Europe/Berlin (+01:00)
}

我正在使用Laravel 5.7,我已经在我的mac和我的共享主机提供商上测试过了。

我不知道自己做错了什么。如果你能帮我,我会很高兴。

问候基督教

这似乎是因为服务器的时区与您自己的时区不同。

这可能是由以下原因引起的:

Server misconfiguration
Physical location of the server is in a different timezone
Policies of your provider could also cause this. If your provider decides they want to operate on the same timezone on every server they have throughout the world, this will cause issues.

服务器的时区显示为CET(中欧时间(,即+1 GMT,如您所述。

要解决此问题,您应该更改php.ini文件中的时区(说明来自链接(:

Open your php.ini file
Add the following line of code to top of your php.ini file:
date.timezone = "US/Central"

或者,如果您希望PHP使用另一个时区,您应该将美国/中央时区替换为此处列出的所需时区。

在laravel上,打开config/app.php并查找"时区"条目。在上面输入您自己的时区。

'timezone' => 'your_timezone_here'

您可能还想运行

php artisan config:cache

更改后。

最新更新