【问题标题】:Laravel Eloquent doesn't display date/time as in database timezoneLaravel Eloquent 不像数据库时区那样显示日期/时间
【发布时间】:2020-07-17 02:08:51
【问题描述】:

我试图在 MySQL 中获取与我的数据相同的时区,但 JSON 响应以不同的时间回复。 我尝试使用 Carbon,但结果始终显示相同(与我的时区不同)。 另外,我的时区在app.php'timezone' => 'Asia/Kuala_Lumpur',将数据存储为我的国家时区。

$todayentry = SensorTissue::whereDate('entryDate', Carbon::now()->setTimezone(config('app.timezone')))->get()->sortBy('entryDate');
$todayentry = json_decode($todayentry);
return $todayentry;

json 响应的结果:

[{
"tsID": 10,
"entryDate": "2020-04-04T23:57:17.000000Z",
"sensorValue": 7
},
{
"tsID": 11,
"entryDate": "2020-04-04T23:57:34.000000Z",
"sensorValue": 6
}]

回复显示2020-04-04T23:57:34.000000Z 在我的数据库中应该是2020-04-05 07:57:34

【问题讨论】:

    标签: php json laravel eloquent php-carbon


    【解决方案1】:

    尝试替换以下代码行:

    $todayentry = SensorTissue::whereDate('entryDate', Carbon::now()->setTimezone(config('app.timezone')))
                              ->get()->sortBy('entryDate');
    

    使用以下代码:

    $timeZone = config('app.timezone'); //change over here
    $todayentry = SensorTissue::whereDate('entryDate', Carbon::now($timeZone))
                              ->get()->sortBy('entryDate');
    

    【讨论】:

    • 2.我将 MySQL 全局和会话时区更改为 +8.00,它也不起作用。我认为这是来自 JSON 本身,可能有需要修改的默认时区设置?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2021-07-09
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多