任务时间: 2025年06月21日 20:38
任务类型: 功能扩展
模块: UCore/DcatAdmin
在UCore的GridHelper类中新增columnDateTime助手方法,用于统一后台列表中的时间显示格式为"年-月-日 时:分:秒"。
UCore/DcatAdmin/GridHelper.phpcolumnDateTime($field, $label = '')Y-m-d H:i:s 格式-// 时间戳
1640995200 → 2022-01-01 08:00:00
// Carbon实例
Carbon::parse('2023-06-15 14:30:45') → 2023-06-15 14:30:45
// DateTime对象
new DateTime('2023-12-25 23:59:59') → 2023-12-25 23:59:59
// 字符串
'2023-07-20 10:15:30' → 2023-07-20 10:15:30
// 空值
null 或 '' → -
// 零值
0 → 1970-01-01 08:00:00
protected function grid()
{
$grid = $this->gridMake();
$helper = $this->gridHelper($grid);
// 使用新的columnDateTime方法
$helper->columnDateTime('created_at', '创建时间');
$helper->columnDateTime('updated_at', '更新时间');
$helper->columnDateTime('login_time', '登录时间');
return $grid;
}
tests/Unit/UCore/GridHelperDateTimeTest.phpvendor/bin/phpunit tests/Unit/UCore/GridHelperDateTimeTest.php
# OK (1 test, 7 assertions)
docs/GridHelper-columnDateTime使用示例.mdgit add .
git commit -m "新增UCore GridHelper columnDateTime方法
- 在UCore/DcatAdmin/GridHelper.php中新增columnDateTime方法
- 统一后台列表时间格式为'年-月-日 时:分:秒'格式
- 支持时间戳、Carbon实例、DateTime对象、字符串等多种时间类型
- 自动处理空值显示为'-',添加sortable功能
- 包含完整的单元测试验证各种输入类型
- 添加详细的使用示例文档"
git push
UCore/DcatAdmin/GridHelper.phptests/Unit/UCore/GridHelperDateTimeTest.phpdocs/GridHelper-columnDateTime使用示例.md// 检查空值(但不包括0,因为0是有效的时间戳)
if (is_null($value) || $value === '') {
return '-';
}
columnCreatedAt()和columnUpdatedAt()方法,使其使用统一的时间格式化columnTimes()组合时间列方法,将创建时间和更新时间组合显示vendor/bin/phpunit tests/Unit/UCore/GridHelperDateTimeTest.php
# OK (2 tests, 13 assertions)
成功优化了后台创建时间/修改时间的展示,实现了:
该优化提高了后台管理界面的一致性和用户体验,为项目的时间显示标准化奠定了基础。