Search code examples
laravelphp-carbon

Arabic Date is not showing in Laravel using Carbon


i want to show date and days in Arabic format using Carbon. but it's showing in English.

Carbon::setlocale("ar");
echo Carbon::parse()->format('D d F Y');

Result: Sun 12 May 2019

Expected result: it should show day and month in arabic.


Solution

  • Carbon::setLocale() is only for the diffForHumans method, otherwise it uses the gloabl PHP Datetime locals. So if you want to use Arabic you have to call

    setLocale(LC_TIME, $locale);
    

    then use Carbon method formatlocalized()

    Carbon\Carbon::now()->formatLocalized($format);
    

    Note that PHP recognizes more than one Arabic locale so you'll have to choose one from this list

    Just make sure you pick one that's installed on the server you're working on or the setLocale() method will fail and return false.