Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasosho's avatar

formatting date from a a query as 25 Mar, 2022

How do i correctly format date fromat query? format 25 Mar, 2022

$group = DB::table('appointments')
            ->Where('doctor_id', $user)
            ->select(DB::raw('DATE(date) as date'), DB::raw('count(date) as count'))
            ->groupBy('date')->format('%d-%m-%Y')
            ->get();
0 likes
12 replies
Nakov's avatar

Try this

$group = DB::table('appointments')
            ->where('doctor_id', $user)
            ->select(DB::raw('DATE(date) as date'), DB::raw('count(date) as count'))
            ->groupBy('date')
            ->get()
    ->map(function ($row) {
    $row['date'] = Carbon::parse($row->date)->format('d M, y');
    return $row;
});
2 likes
tomasosho's avatar

@MichalOravec I get this error, i don't know if it's peculiar to laravel 8

My date is a column under the appointments table and it's getting the values.. all tested!

"message": "SQLSTATE[42703]: Undefined column: 7 ERROR:  column \"%d %b, %Y\" does not exist\nLINE 1: select DATE_FORMAT(date, \"%d %b, %Y\") as date, COUNT(date) a...\n                                 ^ (SQL: select DATE_FORMAT(date, \"%d %b, %Y\") as date, COUNT(date) as count from \"appointments\" where \"doctor_id\" = 1 group by \"date\")",
jlrdw's avatar

@tomasosho you could also get results with regular date and format for display in vue, via some js or moment js.

Please or to participate in this conversation.