如何制作像facebook(3天前等)的日期/时间计数

时间:2017-10-18 02:31:24

标签: ajax laravel-5.1

我是php的新手,而且我是laravel的新手。无论如何,'有问题。在一些文献中,人们使用“diffForHuman”。不幸的是,当我使用它时,我总是得到错误500。我想像facebook那样计算日期/时间,例如:“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",
    },
    },
});

}

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

1 个答案:

答案 0 :(得分:1)

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