Laravel Eloquent不显示数据库时区中的日期/时间

问题描述 投票:0回答:1

我正在尝试获取与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个回答
0
投票

尝试替换下面的代码行:

$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');
© www.soinside.com 2019 - 2024. All rights reserved.