Articles

How to display Laravel localized time in multi-Lang Laravel website

In this example we will be using PHP carbon library with PHP functions to display Laravel localized time based on language selected in Laravel website. We will converting time for eight languages, you can extend for languages you want to show time for. All these examples are inspired from Scratch Coding Laravel Coding

Languages

  • English
  • Arabic
  • German
  • Spanish
  • French
  • Hindi
  • Portuguese
  • Russian
  • Ukrainian

How to force HTTPS on Laravel

100 Laravel Interview Questions

Laravel Localized time

First of all we will get selected language for a multi-Lang laravel website and store it in a variable.

$locale = app::getLocale();

Now based on selected language, We will use switch case to find selected language and display time

switch ($locale) {
    case 'en':
        $locale_str = "en_US.UTF-8";
        $locale_lctime = "en_US";
        break;
    case 'ar':
        $locale_str = "ar_AE.utf8";
        $locale_lctime = "ar_AE";
        break;
    case 'de':
        $locale_str = "de_DE.UTF-8";
        $locale_lctime = "de_DE";
        break;
    case 'es':
        $locale_str = "es_ES.UTF-8";
        $locale_lctime = "es_ES";
        break;
    case 'fr':
        $locale_str = "fr_FR.UTF-8";
        $locale_lctime = "fr_FR";
        break;
    case 'in':
        $locale_str = "hi_IN.UTF-8";
        $locale_lctime = "hi_IN";
        break;
    case 'pt':
        $locale_str = "pt_PT.UTF-8";
        $locale_lctime = "pt_PT";
        break;
    case 'ru':
        $locale_str = "ru_RU.UTF-8";
        $locale_lctime = "ru_RU";
        break;
    case 'it':
        $locale_str = "it_IT.UTF-8";
        $locale_lctime = "it_IT";
        break;
    default:
        $locale_str = "en_US.UTF-8";
        $locale_lctime = "en_US";
}

setlocale(LC_ALL, $locale_str);
$startDate = strftime("%A, %F", strtotime(\Carbon\Carbon::now()->subDays(6)->format('Y-m-d')));
$endDate = strftime("%A, %F", strtotime(\Carbon\Carbon::now()->format('Y-m-d')));

Now we time in local language selected in laravel, we will display it in blade.

{{ $startDate }} to {{ $endDate }}
CADSLIST SEO BLOG

Recent Posts

The Best Spas in Dubai for the Ultimate Luxury Experience

A list of the best spas in Dubai that will relax your body and mind…

October 3, 2023

12 of The Best Make-Up Brands Available in Canada

Canada is a majestically beautiful country full of natural wonders and is also home to…

September 22, 2023

10 Best Makeup Brands in the United States

Here is a list of the Best USA Makeup Brands in 2023, that are safe…

September 20, 2023

The Best Australian Makeup Brands That You Need To Try

Here is a list of the Best Australian Makeup Brands in 2023, that are safe…

September 10, 2023

How to Create TikTok Content that can go Viral Easily?

Content creators always try hard to make their content reach a large number of followers…

September 8, 2023

Top Job Websites for Truck Drivers in Canada

Truck companies are constantly searching for people to fulfill their logistics, transportation, and supply chain…

August 11, 2023