How to get the month in Portuguese using Carbon?

Asked

Viewed 3,565 times

4

I’m having doubts on how to get the month’s value in full in Portuguese using the API of Carbon using the framework Laravel. Initially, I built this logic using PHP.

        if($data->month == 1){
            $mes = 'Janeiro';
        }

This implies doing around 12 if’s to be able to get the value of the month in Portuguese. Assuming that the variable $data receives the value of the time of now.

        $data = Carbon::now();   

How could you be taking the month in full in Portuguese using only functions of API of Carbon? As shown in the example below.

        $mes = $data->localeMonth;

For example, today is caught the month "July". I am also in doubt on how I can change the location to the official time of Brasilia. I believe that changing the location, can solve my problem. But it is difficult to find in the documentation of API the solution. Could you help me?

Carbon Documentation

  • Carbon::now()->settings(['locale' => 'pt_BR', 'timezone' => 'America/Sao_Paulo',]); changes the locale?

  • error appears "Method Settings does not exist."

  • Which version of Laravel vc uses? and which version of Carbon?

  • Carbon 1.26.3 and Laravel 5.8, maybe 5.7, I’m not sure

  • In the documentation there is a part that talks about internationalizing with Cabon 1.x which basically uses the function strftime() that may have changed locale different from the Datetime class.

  • where can I find this in the documentation?

  • No like is in question search for version 1 documentation of Localization by clicking here. there are some examples.

  • I tried using Carbon::executeWithLocale(), Carbon::setLocale('de'), and called with $data->formatLocalized('%B'), but persists in displaying the month in English. I don’t know if you could help me, where I can get all the locations I can use for testing.

Show 3 more comments

1 answer

7


The Carbon documentation notifies on the localization for version 1 of the API:

Unfortunately the base class DateTime does not have any localization support. To Begin localization support a formatLocalized($format) method was Added. The implementation makes a call to strftime using the Current instance timestamp. If you first set the Current locale with PHP Function setlocale() then the string returned will be Formatted in the correct locale.

The native object of PHP DateTime does not support internationalization and it is the base class of Carbon. But the documentation itself provides code to assist in this problem. Follow the code below adapted to your situation.

setlocale(LC_TIME, 'ptb'); // LC_TIME é formatação de data e hora com strftime()
$dt = Carbon::now();
echo $dt->formatLocalized('%A %d %B %Y');

If the above instruction still doesn’t work, you can "force" internationalization as follows:

// Force locale
date_default_timezone_set('America/Sao_Paulo');
setlocale(LC_ALL, 'pt_BR.utf-8', 'ptb', 'pt_BR', 'portuguese-brazil', 'portuguese-brazilian', 'bra', 'brazil', 'br');
setlocale(LC_TIME, 'pt_BR.utf-8', 'ptb', 'pt_BR', 'portuguese-brazil', 'portuguese-brazilian', 'bra', 'brazil', 'br');


// Create Carbon date
$dt = Carbon::now();
echo $dt->formatLocalized('%A %d %B %Y');
  • 1

    The above instruction did not work, but the bottom one worked well. Thank you very much for the answer.

  • I’m glad I could help you! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.