3 votes

Get month name in Spanish (Laravel, Carbon)

Does anyone know how to get the name of the month of a date in Spanish using Carbon y Laravel .

I currently use:

$fecha = \Carbon\Carbon::parse($data["Fecha"])
$mes = $fecha->format("F");

But it returns the name of the month in English.

I have also tried with esto

6voto

C47 Points 159

I think the most feasible solution is this:

The options you provided me are not working, and it is really urgent.

$meses = array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
$fecha = Carbon::parse($inputs['Fecha']);
$mes = $meses[($fecha->format('n')) - 1];
$inputs['Fecha'] = $fecha->format('d') . ' de ' . $mes . ' de ' . $fecha->format('Y');

0 votes

I recommend you to check @Dev.Joel's answer because I have implemented it and it worked for me.

0 votes

It doesn't work for me, I would like to upload an image but I don't know how to do it here to show you that it doesn't work for me.

0 votes

@AlfredoPaz already modified the question so you can see the image.

5voto

I solved it by adding in the AppServiceProvider

public function boot()
{
    // Configuración para fechas en español
    Carbon::setUTF8(true);
    Carbon::setLocale(config('app.locale'));
    setlocale(LC_ALL, 'es_MX', 'es', 'ES', 'es_MX.utf8');

}

Carbon::setLocale establishes the language for diffForHumans()

setlocale establishes the language for formatLocalized('%B') but it only works if any of the listed localizations are installed on the server. In the case of Ubuntu 18.04 the language 'es_MX.utf8' is installed. Guide to install languages in linux

4voto

Dev. Joel Points 20672

Carbon uses the static method setLocale only for some of its methods such as diffForHumans() but not for format i.e.

Carbon::setLocale('es');
$fecha = Carbon::parse('03-04-2018');
$fecha->diffForHumans(); //esto se mostrará en español
$fecha->format("F"); // Inglés.

To perform the formatting you must do the following setLocale as follows, but the method format will still be in the same language, you should use the method formatLocalized that makes use of strftime in addition to adding the . format you want (%B for month name)

setlocale(LC_ALL, 'es_ES');
$fecha = Carbon::parse('03-04-2018');
$fecha->format("F"); // Inglés.
$mes = $fecha->formatLocalized('%B');// mes en idioma español

you can also review the next package Jenssegers/date for language in dates with Laravel.

1 votes

Joel just a comment, I should have left the following line like this, otherwise it would indicate that the Carbon class does not exist $fecha = Carbon\Carbon::parse('03-04-2018');

0 votes

@AlfredoPaz you must import the class with use use Carbon\Carbon; If you use the option you say there is no need to perform the import, thanks for the comment.

0 votes

yep I know I just wanted to make the comment to you

2voto

diego orellana Points 333

Hello, try the following:

setlocale(LC_ALL,"es_ES"); 
\Carbon\Carbon::setLocale('es'); 
$fecha = \Carbon\Carbon::parse($data["Fecha"])
$mes = $fecha->format("F");

Ojo If you have a configuration file, try to call the setlocale method from there.

0 votes

Yes, there is a configuration file, however I have already tried what you mention but it does not work.

0 votes

Try entering the configuration file: /app/config/app.php and change the property 'locale' => 'en',

0 votes

is already in es

1voto

Alejandra Points 11

I know it's too late, I tried everything like you and in the end I found several little things, one of them is to have the locale in the language you want, check it out. app.php locale is

then in AppServiceProvider.php

I added this in the function boot Carbon::setLocale(config('app.locale'));

and later in my controller

$fecha = Carbon::parse($fSalida);
$date = $fecha->locale(); //con esto revise que el lenguaje fuera es 
dd($fecha->monthName); //y con esta obtengo el mes al fin en español!

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X