如何仅提取时间字段中的时、分、秒(HH:MM:SS格式)

技术百科 霞舞 发布时间:2026-01-28 浏览:

本文介绍在 laravel eloquent 查询中,使用 mysql 的 `date_format()` 函数配合 `db::raw()`,从 datetime 类型字段中精准提取并格式化为“小时:分钟:秒”(如 `14:23:05`)的方法。

在实际开发中,数据库表(如 attendance)的 attendance_time 字段通常为 DATETIME 类型,存储完整的时间戳(例如 '2025-05-20 08:45:32')。但业务场景常只需展示“时:分:秒”部分(如考勤打卡时间),此时直接 SELECT in.attendance_time AS in_time 会返回完整日期时间,不符合前端简洁展示需求。

Laravel 原生查询构建器不支持对字段进行原生时间格式化,需借助 DB::raw() 调用底层数据库函数。MySQL 提供了强大的 DATE_FORMAT() 函数,可按指定格式截取并格式化时间部分。

✅ 正确做法是:将原 select() 中的 'in.attendan

ce_time as in_time' 替换为:

DB::raw('DATE_FORMAT(in.attendance_time, "%H:%i:%s") as in_time'),
DB::raw('DATE_FORMAT(out.attendance_time, "%H:%i:%s") as out_time')

注意:格式符说明:

  • %H → 24 小时制小时(00–23)
  • %i → 分钟(00–59)
  • %s → 秒(00–59)
  • 冒号 : 作为分隔符(也可替换为 - 或空格,如 "%H-%i-%s")

完整修正后的查询片段如下:

$absen = DB::table('attendance as in')
    ->where('in.in_out', 'in')
    ->where('in.company_id', \Session::get('selected_company'))
    ->whereDate('in.created', Carbon::today())
    ->leftJoin('attendance as out', function ($join) {
        $join->on('in.employee_id', 'out.employee_id')
            ->where('out.in_out', 'out')
            ->where('out.company_id', \Session::get('selected_company'))
            ->whereDate('out.created', Carbon::today());
    })
    ->join('employee', 'employee.id', 'in.employee_id')
    ->join('location_library', 'location_library.id', 'in.attendance_location_id')
    ->join('company as cp', 'cp.id', 'in.company_id')
    ->join('employee_in_app as e_app', 'e_app.employee_id', 'in.employee_id')
    ->join('employee_in_company', 'in.employee_id', 'employee_in_company.employee_id')
    ->select(
        'employee.name',
        'cp.alias',
        'in.employee_id',
        'location_library.location_name',
        DB::raw('DATE_FORMAT(in.attendance_time, "%H:%i:%s") as in_time'),
        DB::raw('DATE_FORMAT(out.attendance_time, "%H:%i:%s") as out_time'),
        'e_app.note'
    )
    ->orderBy('in.attendance_time', 'DESC')
    ->get();

⚠️ 注意事项:

  • 此方案依赖 MySQL 数据库;若使用 PostgreSQL,应改用 TO_CHAR(in.attendance_time, 'HH24:MI:SS');
  • DATE_FORMAT() 返回字符串类型,不可再用于时间计算或排序(如需排序原始时间,请保留原始字段或额外 SELECT in.attendance_time);
  • 若字段可能为 NULL(如员工尚未打卡),DATE_FORMAT(NULL, ...) 返回 NULL,符合预期,无需额外处理;
  • Laravel 10+ 中确保已正确引入 use Illuminate\Support\Facades\DB;。

通过该方式,既保持了查询性能(格式化在数据库层完成),又精准满足了 UI 层对纯时间格式的展示需求。


# 能为  # 只需  # 也可  # 如需  # app  # 不支持  # 再用  # ui  # 字符串  # 数据库  # red  # NULL  # 前端  # session  # cad  # select  # mysql  # postgresql  # 不符合  # 分隔符  # laravel  # 字符串类型  # 可按  # 将原 


相关栏目: <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 AI推广<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 SEO优化<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 技术百科<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 谷歌推广<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 百度推广<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 网络营销<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 案例网站<?muma echo $count; ?> 】 <?muma $count = M('archives')->where(['typeid'=>$field['id']])->count(); ?> 【 精选文章<?muma echo $count; ?>

相关推荐

在线咨询

点击这里给我发消息QQ客服

在线咨询

免费通话

24h咨询:4006964355


如您有问题,可以咨询我们的24H咨询电话!

免费通话

微信扫一扫

微信联系
返回顶部