Convert month number to name

7

I need to convert the number of the month to the name of the same, but it has to be in Portuguese (and preferably without the need of substr )

I can do this, with the default language in English

$monthNum  = 3;
$dateObj   = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
echo substr($monthName, 0, 3);
// output Mar

online test

Is there a way to change the language type to pt-Br to use this function?

I do not want to create a array() containing all the months already abbreviated and in Portuguese. I want a solution in-built

    
asked by anonymous 17.06.2014 / 15:57

4 answers

8

With the help of comment from @ lost where it indicates the issue # I have made the following modifications to access the date I need (note that the

17.06.2014 / 16:11
3

Although there is already a response, it is noted that with respect to location, the DateTime class is flagged and does not pay attention to what has been defined, it is even necessary to use strftime ()

In your case, to return the full textual representation would look like this:

strftime( '%B', $dateObj -> getTimestamp() );

I've highlighted the term complete because you use a substr () to shorten the output. In that case, it would be more interesting to use:

strftime( '%b', $dateObj -> getTimestamp() );

And get the correct abbreviated representation.

    
17.06.2014 / 16:35
2

The answers already present are the way forward, but for those who are having trouble setting locale to Portuguese, a scenario that is sometimes impossible or limited due to server settings, a function that does not rely on the settings provided by setlocale , doing a verification of the received values and returning the date formatted in Portuguese:

Function in PHP

/**
 * Converter TimeStamp para data em Português
 *
 * @param integer $timestamp Unix timestamp
 * @param boolean $hours Se "true" devolve também as horas
 * @param string $timeZone Zona a utilizar para gerar as horas
 *
 * @return string
 */
function dataEmPortugues ($timestamp, $hours = FALSE, $timeZone = "Europe/Lisbon") {

    $dia_num = date("w", $timestamp);// Dia da semana.

    if($dia_num == 0){
    $dia_nome = "Domingo";
    }elseif($dia_num == 1){
    $dia_nome = "Segunda";
    }elseif($dia_num == 2){
    $dia_nome = "Terça";
    }elseif($dia_num == 3){
    $dia_nome = "Quarta";
    }elseif($dia_num == 4){
    $dia_nome = "Quinta";
    }elseif($dia_num == 5){
    $dia_nome = "Sexta";
    }else{
    $dia_nome = "Sábado";
    }

    $dia_mes = date("d", $timestamp);// Dia do mês

    $mes_num = date("m", $timestamp);// Nome do mês

    if($mes_num == 01){
    $mes_nome = "Janeiro";
    }elseif($mes_num == 02){
    $mes_nome = "Fevereiro";
    }elseif($mes_num == 03){
    $mes_nome = "Março";
    }elseif($mes_num == 04){
    $mes_nome = "Abril";
    }elseif($mes_num == 05){
    $mes_nome = "Maio";
    }elseif($mes_num == 06){
    $mes_nome = "Junho";
    }elseif($mes_num == 07){
    $mes_nome = "Julho";
    }elseif($mes_num == 08){
    $mes_nome = "Agosto";
    }elseif($mes_num == 09){
    $mes_nome = "Setembro";
    }elseif($mes_num == 10){
    $mes_nome = "Outubro";
    }elseif($mes_num == 11){
    $mes_nome = "Novembro";
    }else{
    $mes_nome = "Dezembro";
    }
    $ano = date("Y", $timestamp);// Ano

    date_default_timezone_set($timeZone); // Set time-zone
    $hora = date ("H:i", $timestamp);

    if ($hours) {
        return $dia_nome.", ".$dia_mes." de ".$mes_nome." de ".$ano." - ".$hora;
    }
    else {
        return $dia_nome.", ".$dia_mes." de ".$mes_nome." de ".$ano;
    }
}

Example of use:

// data actual
echo dataEmPortugues(time());

// uma outra data
echo dataEmPortugues(strtotime("2014-07-17 21:49:23"));

// com data e hora
echo dataEmPortugues(strtotime("2014-07-17 21:49:23"), TRUE);

// com data e hora São Paulo
echo dataEmPortugues(strtotime("2014-07-17 21:49:23"), TRUE, "America/Sao_Paulo");

Example Result

View Online Example - PHP Sandbox

// data actual
// Terça, 17 de Junho de 2014

// uma outra data
// Quinta, 17 de Julho de 2014

// com data e hora
// Quinta, 17 de Julho de 2014 - 21:49

// com data e hora São Paulo
// Quinta, 17 de Julho de 2014 - 17:49
    
17.06.2014 / 23:08
2

I know it has been asked for a long time, and it has local-based solutions, but as a 2nd option, I would like to put this approach as an example of a solution without logic testing.

I find it a bad practice to nest if 's as if there was no tomorrow.

From this, you can assemble class, function, spindle, etc ... The idea is to exemplify how to do the same thing, without using nested logical tests, or switch 's huge.

    $numero_dia = date('w')*1;
    $dia_mes = date('d');
    $numero_mes = date('m')*1;
    $ano = date('Y');
    $dia = array('Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado');
    $mes = array('', 'Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
    echo $dia[$numero_dia] . ", " .$dia_mes . " de " . $mes[$numero_mes] . " de " . $ano . ".";

Check out: Friday, September 25, 2014.

    
25.07.2014 / 20:02