Home > Software design >  DB::raw is there any way to date_format with language?
DB::raw is there any way to date_format with language?

Time:11-10

$test = Test::query()
            ->select("test.id as id", "users.first_name as first_name", "users.last_name as last_name")
            ->addSelect("test.company_name as company_name", "test.email as email")
            ->addSelect(DB::raw("DATE_FORMAT(test.created_at, '%d-%m-%Y') as created"))
            ->join("users", "users.id", "=", "test.user_id")
            ->where("test.test_id", "=", $test->id)
            ->get();

It is formating datatable dates but it is not giving language format if i made %b it giving just english lang

CodePudding user response:

according to mysql doc:

Beginning with MySQL 5.1.12, the locale indicated by the lc_time_names system variable controls the language used to display day and month names and abbreviations. This variable affects the output from the DATE_FORMAT(), DAYNAME(), and MONTHNAME() functions.

in your case the code would be:

DB::statement("SET lc_time_names = 'ar_BH';"); // or any locale you want, see the supported locales in the same link.

then add -%b:

->addSelect(DB::raw("DATE_FORMAT(test.created_at, '%d-%m-%Y-%b') as created"))
  • Related