php Carbon::now() 没有使用 UTC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40790275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:15:07  来源:igfitidea点击:

Carbon::now() is not using UTC

phplaraveltimezonetimestampphp-carbon

提问by Vahn Marty

I am from Philippines. If ever I will use Carbon::now(), it catches my machine time and not the UTC time.

我来自菲律宾。如果我将使用 Carbon::now(),它会捕获我的机器时间而不是 UTC 时间。

My config/app.php is:

我的 config/app.php 是:

    'timezone' => 'UTC',

This is my code:

这是我的代码:

$log->dateRequest = Carbon::now();

If ever I will post a request at 9:00pm (Philippine time). It catches 21:00:00 , instead of 13:00:00 (from UTC).

如果有的话,我会在晚上 9:00(菲律宾时间)发布请求。它捕获 21:00:00 ,而不是 13:00:00 (来自UTC)。

回答by Alexey Mezenin

In one of you questions you mentioned that you need to use multiple timezones in your app. So, you can add timezone dynamically:

在您提到的一个问题中,您需要在应用程序中使用多个时区。因此,您可以动态添加时区:

Carbon::now('UTC')

Or use setTimezone('UTC')method on existing date.

或使用setTimezone('UTC')现有日期的方法。

回答by Object Manipulator

As stated in Carbon docs instantiation, try this:

碳文档实例化所述,试试这个:

$log->dateRequest = Carbon::now('UTC');

$log->dateRequest = Carbon::now('UTC');

回答by Manh Nguyen

Carbon uses the default DateTimePHP object.

Carbon 使用默认的DateTimePHP 对象。

Gets the default timezone:

获取默认时区:

date_default_timezone_get();
// or
Carbon::now()->timezoneName;

Sets the default timezone

设置默认时区

date_default_timezone_set('UTC');

var_dump(Carbon::now()->utc); // true;