Laravel Get created_at at Millisecond Timestamp

In Laravel 5.1, I'm trying to get a date created_atin a millisecond timestamp to use it in frontend javascript. How can i do this? My request at the moment looks something like this:

$stats = User::where('created_at', '>=', $range)
               ->groupBy('date')
               ->orderBy('date', 'DESC')
               ->get([
                      DB::raw('Date(created_at) as date'),
                      DB::raw('COUNT(*) as value')
                    ])
               ->toJSON();

I want the output to created_atbe as follows:1325356200000

+4
source share
3 answers

If you feel good with help DB::raw, you can try something like DB::raw("CONCAT(UNIX_TIMESTAMP(DATE(created_at)), '000000') as date").

+2
source

You can create an accessor for the field created_atand convert the date and time to a timestamp.

+2

@Neel, Laravel, Carbon PHP DateTime . UNIX_TIMESTAMP , .

Laravel, created_at updated_at Carbon. , ​​ ,

User::findOrFail($id)->created_at->timestamp

"created_at" => "2015-10-07 06:10:49"

, . 1444198249

, .

+1

Source: https://habr.com/ru/post/1616693/


All Articles