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
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 }}