How to change date format in laravel Query Builder from "2016-03-12" to "12-Mar-2016"

GRESPL Nagpur picture GRESPL Nagpur · Mar 10, 2016 · Viewed 48.7k times · Source

How to change date-format in laravel from "2016-03-12" to "12-Mar-2016"

$results = DB::table('customers as cust')
        ->where('cust.id',$id)
        ->select("cust.*","cust.cust_dob as dob")
        ->first();

Should I use laravel raw query.

I tried this,

 ->select("cust.*","DATE_FORMAT(cust.cust_dob, '%d-%M-%Y') as formatted_dob")

Any guide on this please.

Answer

cronLancer picture cronLancer · Mar 10, 2016

Try to use this:

$results = DB::table('customers as cust')
             ->where('cust.id',$id)
             ->select(DB::raw('DATE_FORMAT(cust.cust_dob, "%d-%b-%Y") as formatted_dob'))
             ->first();