关于webman使用Laravel模型读取日期时间相差8小时的问题?

fanyin

问题描述

关于webman使用Laravel模型写入时使用自动时间戳,读取时使用格式化的日期格式相差8小时的问题。


在模型中

//指示模型是否主动维护时间戳。
public $timestamps = true;

protected $dateFormat = 'U';

//创建时间
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 中,也默认配置了 'default_timezone' => 'Asia/Shanghai',

解决方法找到一个

依然在模型中:

protected function serializeDate(DateTimeInterface $date): string
{
return $date->format(Carbon::parse($date)->toDateTimeString());
}

但是感觉这样写有点不靠谱,就不能通过配置文件或者什么来解决吗?请求大家帮忙!

423 2 0
2个回答

小皮蛋

laravel db的确存在这个问题,参考https://blog.csdn.net/Attitude_do_it/article/details/122740434
希望后续能针对laravel提供统一配置方案~

  • 暂无评论
Madman
  • 暂无评论
🔝