首页 > 解决方案 > 在 laravel 中从 UTC 时区将数据显示到 EST 时区

问题描述

我的created_atupdated_at列插入到 UTC 时区。现在我想根据“美国/芝加哥”时区显示数据。我正在使用 Carbon 库来获得结果。

下面是我为从数据库中获取信息而编写的代码。

    public function getAllUsers(){
        $users =  BaseUserModel::
        select('id', 'email', 'created_at')
            ->whereDate(
                'created_at',
                '=',
                Carbon::today('america/chicago')
            )
            ->orderBy('created_at', 'desc')
            ->limit(5)->get();
        return $users;
    }

在此处输入图像描述

附有 UTC 时间的数据库条目。现在,当我尝试执行查询时,只得到一个输出(2018-10-03 15:27:18)。但我的预期输出应该在“美国/时区”两个日期。

查看 UTC 和美国/芝加哥时区的差异。 https://www.worldtimebuddy.com/

标签: phplaravel-5php-carbon

解决方案


您需要使用 UTC 日期/时间进行查询。例如,如果您希望 2018-10-03 0:00:00 CST 到 2018-10-03 23:59:59 CST,则需要使用 2018-10-03 6:00:00 到 2018-10- 进行查询04 5:59:59 取决于时区和夏令时。


推荐阅读