Laravel 7-曾经作为UTC的碳返还 [英] Laravel 7- Carbon return ever time as UTC

查看:51
本文介绍了Laravel 7-曾经作为UTC的碳返还的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个真正的陌生人,当用Carbon返回当前时间时,这对我来说已经是UTC时间了.我不明白为什么我将所有内容都配置为Europe/Madrid.

This is a real stranger, when return the current time with Carbon, this is return ever time as UTC for me. I do not understand why if I config as Europe/Madrid everything.

示例:

return Carbon::now() 
The result is 2020-07-06 14:30:00

但是是:

dd(Carbon::now())

The result is 2020-07-06 16:30:00

我不明白为什么.

我的文件app.php

My file app.php

'timezone' => 'Europe/Madrid',

并且在加载模型(例如用户)时

And when load a model for example user

$user = User::find(1);

created_at和updated_at返回

The created_at and updated_at return

2020-06-07 14:35:00

但是在数据库中保存的值是

But in database the value saved is

2020-06-07 16:35:00

推荐答案

Carbon使用默认的DateTime PHP对象.您可以将自定义时区设置为:

Carbon uses the default DateTime PHP object. You can set custom timezone as :

$date = Carbon::createFromFormat('Y-m-d H:i:s', $tz, 'Europe/Madrid');

AppServiceProvider.php 中,您可以添加php功能来更改整个项目的时间戳:

In the AppServiceProvider.php you can add the php functionality to alter the timestamp for the whole project :

public function boot()
{
    Schema::defaultStringLength(191);
    date_default_timezone_set('Europe/Madrid');
}

这篇关于Laravel 7-曾经作为UTC的碳返还的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆