關(guān)于webman使用Laravel模型寫入時使用自動時間戳,讀取時使用格式化的日期格式相差8小時的問題。
在模型中
//指示模型是否主動維護(hù)時間戳。
public $timestamps = true;
protected $dateFormat = 'U';
//創(chuàng)建時間
const CREATED_AT = 'create_time';
//更新時間字段
const UPDATED_AT = 'update_time';
protected $casts = ['create_time' => 'datetime:Y-m-d H:i:s', 'update_time' => 'datetime:Y-m-d H:i:s', 'delete_time' => 'timestamp' ];
前端在使用模型自動讀取日期格式則相差8小時,webman的 config/app.php 中,也默認(rèn)配置了 'default_timezone' => 'Asia/Shanghai',
解決方法找到一個
依然在模型中:
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format(Carbon::parse($date)->toDateTimeString());
}
但是感覺這樣寫有點(diǎn)不靠譜,就不能通過配置文件或者什么來解決嗎?請求大家?guī)兔Γ?/p>
laravel db的確存在這個問題,參考https://blog.csdn.net/Attitude_do_it/article/details/122740434
希望后續(xù)能針對laravel提供統(tǒng)一配置方案~
timezone.php
AI寫了個測試腳本,自己測試下看下哪里有問題。
<?php
use support\Db;
use support\Model;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/support/bootstrap.php';
if (!Db::schema()->hasTable('timezone_test')) {
// 創(chuàng)建表
Db::statement('
CREATE TABLE timezone_test (
id INT AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
custom_datetime DATETIME NULL
)
');
echo "Table 'timezone_test' created successfully.\n";
}
$timeZones = DB::select('SELECT @@global.time_zone AS global_time_zone, @@session.time_zone AS session_time_zone');
// 輸出結(jié)果
foreach ($timeZones as $timeZone) {
echo "DATABASE Global Time Zone: {$timeZone->global_time_zone}\n";
echo "DATABASE Session Time Zone: {$timeZone->session_time_zone}\n";
}
$current_time = DB::select('SELECT NOW() as `time`');
echo "DATABASE Current Time:" . $current_time[0]->time . "\n";
echo "PHP Timezone: " . date_default_timezone_get() . "\n===========================\n";
class Test extends Model
{
protected $table = 'timezone_test';
public $timestamps = true; // 自動維護(hù) created_at 和 updated_at
protected $fillable = ['custom_datetime'];
}
Db::table('timezone_test')->insert([
'custom_datetime' => date('Y-m-d H:i:s') // PHP 當(dāng)前時間
]);
Test::create([
'custom_datetime' => date('Y-m-d H:i:s')
]);
// 讀取數(shù)據(jù)
$records = Test::all();
foreach ($records as $record) {
echo "ID: {$record->id}, Created At: {$record->created_at}, Updated At: {$record->updated_at}, Custom Datetime: {$record->custom_datetime}\n";
}
模型輸出沒問題,測試腳本追加$records->toArray() 問題也是一樣,
array(4) {
["id"]=>
int(5)
["created_at"]=>
string(27) "2025-01-19T13:45:47.000000Z"
["updated_at"]=>
string(27) "2025-01-19T13:45:47.000000Z"
["custom_datetime"]=>
string(19) "2025-01-19 21:45:47"
}
[5]=>
array(4) {
["id"]=>
int(6)
["created_at"]=>
string(27) "2025-01-19T13:45:47.000000Z"
["updated_at"]=>
string(27) "2025-01-19T13:45:47.000000Z"
["custom_datetime"]=>
string(19) "2025-01-19 21:45:47"
}
}
https://learnku.com/docs/laravel/7.x/upgrade/7445#date-serialization
這個是laravel Db v7 更改的特性,大概是為了國際化,輸出日期數(shù)據(jù)帶有時區(qū)信息,可以按照文檔修改自己要的格式
非常感謝
這個方案試過,單獨(dú)模型沒問題,關(guān)聯(lián)模型輸出還是有問題。
這邊看下是不是放棄內(nèi)置時間戳管理,使用事件觸發(fā)寫入時間戳,輸出時間戳 跟日期雙輸出。