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

Asked

Viewed 127,987 times

120

In my project I’m using the function date(), however I would like it to be in Portuguese, the departure date of the moment is: Thursday 6th, I would like it to be on the same model but in Portuguese, this is my code:

setlocale(LC_ALL, 'pt_BR');
echo date('l jS'); // Thursday 6th

4 answers

168


Approach with strftime

Use strftime() to create the full date, as this function automatically picks up the locale. As quoted by @bfavaretto, just enter 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 set 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'));

exit:

quinta-feira, 06 de março de 2014

%A: day of the week in full.

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

%B: month in full.

%Y: year represented with four digits.

Example - strftime


Approach with Intldateformatter

It is also possible to obtain the same result with the classes Datetime and Intldateformatter. Important the INTL library must be enabled.

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 it is necessary 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 method setPattern() and specify the format according to documentation

Relating:

Use setlocale for date only

  • 1

    @Miguel Format the month separately, use the ucfirst function on it and then put it all together at the end.

  • @Miguel you can use ucwords to leave all initial in uppercase. $data = strftime( '%A, %d %B %Y', strtotime('today'));&#xA;echo ucwords($data);

  • @Miguel you must take the return of strftime() and play in a variable and only in the echo use ucfirst as in the other comment.

  • :) Thank you @rray. It was very useful your reply, I was almost creating a plugin to make these formats in Portuguese.

  • Best answer of all

  • Class parameters are : locale, datetype , timetype, Timezone, Calendar, Pattern. The way it is will not work, as it lacks the Timezone (ex: 'America/Sao_Paulo') before the Calendar IntlDateFormatter::GREGORIAN

  • 1

    Great answer! For those who already have the object Datetime: echo strftime('%A, %d de %B de %Y', $dateTimeObject->getTimestamp());

Show 2 more comments

31

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

On servers configured in Brazilian format, that is, already have Brazilian Portuguese configured in the 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 both together:

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

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

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

  • 1

    That’s correct, my suggestion with date was not (function is not affected by setlocale). Unfortunately I’ve run out of votes for today, I can’t vote on this answer now.

  • no problems, if the answer is useful to @miguel for me is enough

  • Very Obgado... worked @Erlon

23

If none of the other answers work, it may be that the en locales are 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
  • That was my problem. Because of the doubts when setting up the locales I put to allow all, so depending on the application on the server I switch to the necessary.

19

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));

Grudge:

quinta-feira, 21 de dezembro de 2013
Quinta-feira, 21 de Dezembro de 2013

Browser other questions tagged

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