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

时间:2018-12-14 12:56:32

标签: php laravel 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和共享主机提供商上对其进行了测试。

我不知道我在做什么错。 如果您能帮助我,我会很高兴。

问候 基督徒

2 个答案:

答案 0 :(得分:1)

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

这可能是由于:

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使用其他时区,则应使用此处概述的所需时区替换“美国/中央时区”。

答案 1 :(得分:0)

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

'timezone' => 'your_timezone_here'

您可能还想运行

php artisan config:cache

更改后。