如何使 date/time 像 facebook 一样计数(3 天前等)

How to make a date/time count like facebook (3days ago, etc)

我是 php 的新手,当然我也是 laravel 的新手。不管怎样,有问题。在一些文档中,人们使用 "diffForHuman"。不幸的是,我在使用它时总是遇到错误 500。我想像 facebook 一样统计 date/time,例如:“3 天前”等

这是我的控制器:

    public function show(Request $request)
{
 if($request->ajax()){
        $searchs = $request->get('search');
        $search = $searchs['value'];
        \DB::statement(\DB::raw('set @nomor = 0'));

        $index = DB::table('tb_jabatan_karyawan')
                ->leftJoin('tb_jabatan','tb_jabatan.id','=','tb_jabatan_karyawan.id_jabatan')
                ->leftJoin('tb_karyawan','tb_karyawan.id','=','tb_jabatan_karyawan.id_karyawan')
                ->select([
                    \DB::raw('@nomor := @nomor + 1 as no'),

                    'tb_jabatan_karyawan.id as id',
                    'tb_jabatan_karyawan.id_karyawan as id_karyawan',
                    'tb_jabatan_karyawan.id_jabatan as id_jabatan',
                    'tb_karyawan.nama as nama',
                    'tb_karyawan.status_karyawan as status',
                    'tb_jabatan.nama_jabatan as jabatan',
                    'tb_karyawan.tgl_masuk as lama'
                    ]);
            //dd(Carbon::parse($index->lama)->diffForHumans());
        return Datatables::of($index)
            ->edit_column('no', function($index){
                return '<center>'.$index->no.'</center>';
            })
            ->edit_column('nama', function($index){
                return ucwords($index->nama);
            })
            ->edit_column('status', function($index){
                return ucwords($index->status);
            })
            ->editColumn('lama', function ($index) {
            return $index->lama->diffForHumans();
            })
            ->filter(function ($query) use ($request){
                if ($request->get('search')['value']){
                  $query->where('nama', 'like', "%{$request->get('search')['value']}%");
                }
            })
            ->addColumn('action', function($index){
                $tag = "<center><a class='btn btn-info btn-xs' onclick=atur('".$index->id_karyawan."','".$index->id_jabatan."')><i class='fa fa-pencil'></i> Proses</a></center>";
                return $tag;
            })
        ->make(true);
    }

}

这是我的数据表:

    function getData(){
table = $('#table').DataTable({
    dom: "lBfrtip",
    processing: true,
    serverSide: true,
    destroy: true,
    bFilter:true,
    searching: true,
    order: [],
    ajax: base_adm+'kelola/pengunduran-karyawan/show',

    columns: [
        {data: 'no', name: 'no', orderable: true, searchable: false},
        {data: 'nama', name: 'nama', orderable: true, searchable: true},
        {data: 'status', name: 'status', orderable: true, searchable: false},
        {data: 'jabatan', name: 'jabatan', orderable: true, searchable: false},
        {data: 'lama', name: 'lama', orderable: true, searchable: false},
        {data: 'action', name: 'action', orderable: false, searchable: false},
    ],
      "language": {
        "lengthMenu": " _MENU_  Baris",
        "zeroRecords": "Data Tidak Tersedia",
        "info": "Halaman _PAGE_ Dari _PAGES_",
        "infoEmpty": "Data Tidak Tersedia",
        "infoFiltered": "(Hasil penyaringan dari _MAX_ total data)",
        "sSearch": "Pencarian ",
        "oPaginate": {
        "sNext" : "Selanjutnya",
        "sPrevious" : "Sebelumnya",
    },
    },
});

}

我希望有人能帮我解决这个问题。无论如何谢谢

嗯,您的 $index->lama 需要是 Carbon 对象。所以试试 \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $index->lama)->diffForHumans();(假设数据库中的日期以 "Y-m-d H:i:s" 格式存储)