How do the date () function format a date in Portuguese?

95

In my project I am using the date() function, however I would like it to be in Portuguese, my current format is: "Thursday 6th", I would like it to be in the same model but in Portuguese, this is my formatting :

// $article['article_timestamp'] = unix timestamp, time()

setlocale(LC_ALL, 'pt_BR');
echo date('l jS', $article['article_timestamp']);
    
asked by anonymous 06.03.2014 / 19:17

4 answers

130

Approach with strftime

Use strftime () to create the date in full, as this function automatically picks up the locale. As quoted by @bfavaretto, just inform the locale.

strftime() in the words of the manual:

  

Formats a local time / date according to the locale setting.   Name of the month and day of the week and other strings depend on the current location   defined with setlocale ().

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.

Example - strftime

Approach with IntlDateFormatter

You can also get the same result with the DateTime and IntlDateFormatter . Important 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       |

Example - date / time format

In the constructor you need to enter four arguments: locale, date format, time format, timezone, and calendar type

<?php
date_default_timezone_set('America/Sao_Paulo');

$data = new DateTime();
$formatter = new IntlDateFormatter('pt_BR',
                                    IntlDateFormatter::FULL,
                                    IntlDateFormatter::NONE,
                                    'America/Sao_Paulo',          
                                    IntlDateFormatter::GREGORIAN);
echo $formatter->format($data);

Example - IntlDateFormatter

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

Relates to:

Use setlocale for date only

    
06.03.2014 / 19:53
23

By changing the location of PHP you can acquire the properties of a particular country. To change these properties we will use the setlocale() function.

On servers configured in Brazilian format, that is, that already has Brazilian Portuguese configured in locale , only setlocale(LC_ALL, NULL); would suffice. On foreign servers, maybe just setlocale(LC_ALL, 'pt_BR'); would be enough. To increase compatibility try using the two together:

<?php
    setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, 'pt_BR');  
    print ucfirst(gmstrftime('%A'));
?>

The function used to pull the date data is gmstrftime() because it takes the place into consideration. The return of this function is the day of the week, in full, in Portuguese! :)

LC_ALL is a constant indicating that local information will be defined in all instances. LC_ALL includes LC_COLLATE , LC_CTYPE , LC_MONETARY , LC_NUMERIC and LC_TIME .

    
06.03.2014 / 19:38
16

If none of the other answers work, it may be that the local pt-br is not installed on the server (which was my problem). In this case, if your server is Linux Ubuntu, you need to run:

sudo apt-get install language-pack-pt 
sudo dpkg-reconfigure locales
    
28.04.2016 / 16:03
13

Use:

setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');

$var_DateTime = SUA DATA DO BANCO //No meu caso tipo ISO do mongoDB... aí uso o "->sec"

// Caso não queira letras maiúsculas no início de algumas palavras, pode ser usado apenas assim:
echo utf8_encode(strftime('%A, %d, de %B de %Y', $var_DateTime->sec))

//utf8_encode para tratar os caracteres especiais, caso precise
//ucwords para colocar a primeira letra maiúscula

echo utf8_encode(ucwords(strftime('%A', $var_DateTime->sec)).', '.strftime('%d', $var_DateTime->sec).' de '.ucwords(strftime('%B', $var_DateTime->sec)).' de '.strftime('%Y', $var_DateTime->sec));

Resolved:

quinta-feira, 21 de dezembro de 2013
Quinta-feira, 21 de Dezembro de 2013
    
11.12.2014 / 17:10