php - Laravel 创建日期返回不同的格式
问题描述
我尝试从数据库中选择create_data,数据库中的日期格式类似于“2021-05-18 11:06:01”,但是当我检索并显示它时,它会显示类似于“2021-05”的格式-18T03:06:01.000000Z”。有谁知道为什么会变成这样?我想要实际上类似于数据库存储的日期,即“2021-05-18 11:06:01”。但是,数据库中的 create_date 格式为 UTC +8,但接收时会显示 UTC 格式。
数据返回
[0] => Array (
[id] => 1
[log_name] => login
[description] => login
[subject_type] => App\Models\Users
[subject_id] => 0
[causer_type] => App\Models\User
[causer_id] => 2
[properties] => ""
[created_at] => 2021-05-18T03:06:01.000000Z
);
代码
$lastLoggedActivity = ActivityLog::where('causer_id', $userid)
->orWhereIn('subject_id', $selectparentid)
->with('getLogType')
->with('getCauserDetails')
->orderBy('created_at', 'desc')
->get()
->toArray();
解决方案
在 ActivityLog 模型中添加这个,
use Carbon\Carbon;
public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->format('Y-m-d H:i:s');
}
推荐阅读
- firebase - 数据库未在 snapshot.forEach 中为云功能定义
- php - Google Scholar 检测到我想用 PHP 中的 curl 抓取它
- react-native - React Native:任务':app:transformDexArchiveWithExternalLibsDexMergerForDebug'的执行失败
- java - 为 jcombobox 设置默认选项
- java - java.lang.NoClassDefFoundError: org/apache/tools/ant/util/ReaderInputStream javaFx+intelliJ
- node.js - NodeJS 嵌套承诺
- php - 在 centos 的现有 apache 服务器(生产)上运行 laravel 项目
- javascript - 如何像在 IE 中一样在 Chrome 中禁用 A 超链接
- android - 在片段中比较两次不起作用
- reactjs - 如何在 Flatlist 中动态设置 react-native-material-menu