Green Files

How to make the date() function format a date in another language?

Day: 06/01/2022 - Time: 11:56:21

The original language of the date function date() is english, like for exemple Thursday 6th. Here's what to do to change the data's language:

Use strftime() to create the date in full, as this function automatically uses the locale. You'll just have to inform the locale

setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese'); date_default_timezone_set('America/Sao_Paulo'); echo strftime('%A, %d de %B de %Y', strtotime('today'));

Output:

quinta-feira, 06 de março de 2014

%A: day of the week in full.

%d: day of the month represented with two digits.

%B: month in full.

%Y: year represented with four digits.

Approach with IntlDateFormatter

It is also possible to obtain the same result with the DateTime and IntlDateFormatter classes. It is important that the INTL library must be enabled.

The date and time formats are:

Data Constante |Saida FULL |segunda-feira, 1 de setembro de 2014 TRADITIONAL|segunda-feira, 1 de setembro de 2014 LONG |1 de setembro de 2014 MEDIUM |01/09/2014 SHORT |01/09/14 NONE | Hora Constante |Saida FULL |03h00min00s GMT+00:00 TRADITIONAL|03h00min00s GMT+00:00 LONG |03h00min00s GMT+00:00 MEDIUM |03:00:00 SHORT |03:00 NONE |

In the constructor it is necessary to inform four arguments: locale, date format, time format, timezone and the calendar type

format($data);

For a custom output use the setPattern() method and specify the format as per the documentation

Reference:

https://pt.stackoverflow.com/questions/8317/como-fazer-a-fun%c3%a7%c3%a3o-date-formatar-uma-data-em-portugu%c3%aas

GO UP
GO TO INDEX