Date in Portuguese returning in English

Asked

Viewed 834 times

0

I am using this code to return the current date in English (locally):

<?
setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');
date_default_timezone_set('America/Sao_Paulo');
echo utf8_encode(strftime('%A, %d de %B de %Y', strtotime('today')));
//domingo, 22 de outubro de 2017
?>

However in production is returning, changing this line, otherwise does not load, so I removed the utf8_encode:

echo strftime('%A, %d de %B de %Y', strtotime('today'));

Sunday, 22 de October de 2017

The server operating system is America/Sao_Paulo

Any hint?

2 answers

1

Actually the error occurs in the operating system:

I solved by changing the language with the following command on promt debian:

export LANG=pt_BR.UTF-8

Here’s the tip, I’ve tried so many ways.

0

One way to solve the problem is also to use a class that translates the string Mes.

Create a date array with the function explode php native.

$data = explode('/', date('d/m/Y'));

and then take this array and print the current month.

function

function Mes($nomemes, $formato = 'extenso'){
$formatoValido = array('extenso', 'abreviado');

if(!in_array($formato, $formatoValido)){
    return "Formato de data invalido";
}

$mes = array(
1 => array(
    'abreviado' => 'Jan',
    'extenso'   => 'Janeiro'
    ),
2 => array(
    'abreviado' => 'Fev',
    'extenso'   => 'Fevereiro'
    ),
3 => array(
    'abreviado' => 'Mar',
    'extenso'   => 'Março'
    ),
4 => array(
    'abreviado' => 'Abr',
    'extenso'   => 'Abril'
    ),
5 => array(
    'abreviado' => 'Mai',
    'extenso'   => 'Maio'
    ),
6 => array(
    'abreviado' => 'Jun',
    'extenso'   => 'Junho'
    ),
7 => array(
    'abreviado' => 'Jul',
    'extenso'   => 'Julho'
    ),
8 => array(
    'abreviado' => 'Ago',
    'extenso'   => 'Agosto'
    ),
9 => array(
    'abreviado' => 'Set',
    'extenso'   => 'Setembro'
    ),
10 => array(
    'abreviado' => 'Out',
    'extenso'   => 'Outubro'
    ),
11 => array(
    'abreviado' => 'Nov',
    'extenso'   => 'Novembro'
    ),
12 => array(
    'abreviado' => 'Dez',
    'extenso'   => 'Dezembro'
    )
);
return $mes[$nomemes][$formato];
}

In this function I created two ways to call the same string in full and abbreviated as follows

echo "São Paulo, ".$data[0]." de ".Mes($data[1])." de ".$data[2];//string no extenso São Paulo, 22 de Outubro de 2017

And abbreviated value

echo "São Paulo, ".$data[0]." de ".Mes($data[1],'abreviado')." de ".$data[2];// abreviado para São Paulo, 22 de Out de 2017

Browser other questions tagged

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