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
@Miguel Format the month separately, use the ucfirst function on it and then put it all together at the end.
– utluiz
@Miguel you can use
ucwords
to leave all initial in uppercase.$data = strftime( '%A, %d %B %Y', strtotime('today'));
echo ucwords($data);
– rray
@Miguel you must take the return of
strftime()
and play in a variable and only in theecho
useucfirst
as in the other comment.– rray
:) Thank you @rray. It was very useful your reply, I was almost creating a plugin to make these formats in Portuguese.
– Guilherme Soares
Best answer of all
– kokbira
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 CalendarIntlDateFormatter::GREGORIAN
– Erlon Charles
Great answer! For those who already have the object Datetime:
echo strftime('%A, %d de %B de %Y', $dateTimeObject->getTimestamp());
– Lucas