problems with date conversion with php

0

I have the following function that transforms timestamp into a readable date in Portuguese

function dataEmPortugues ($timestamp, $hours = TRUE, $timeZone = "America/Sao_Paulo") {
  $timestamp=strtotime($timestamp);
    $dia_num = date("w", $timestamp);// Dia da semana.

    if($dia_num == 0){
      $dia_nome = "Domingo";
    }elseif($dia_num == 1){
      $dia_nome = "Segunda-feira";
    }elseif($dia_num == 2){
      $dia_nome = "Terça-feira";
    }elseif($dia_num == 3){
      $dia_nome = "Quarta-feira";
    }elseif($dia_num == 4){
      $dia_nome = "Quinta-feira";
    }elseif($dia_num == 5){
      $dia_nome = "Sexta-feira";
    }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." às ".$hora;
    }
    else {
      return $dia_nome.", ".$dia_mes." de ".$mes_nome." de ".$ano;
    }
  }

I have a time stamp

2016-11-30 15:12:09

and when I convert using this function it returns

Quarta-feira, 30 de Novembro de 2016 às 13:12

It is returning 2 hours apart. How do I get this?

    
asked by anonymous 30.11.2016 / 18:14

2 answers

2

I took the liberty of tinkering with the code pattern and removing redundancies. But I kept the structure from being too invasive.

An interesting point is that from PHP7 the excerpt with } elseif ($mes_num == 08) { could cause the parse error Invalid numeric literal .

  

Before, literal occassions that contained invalid numbers were   silently truncated (0128 would become 012). Now, an octal   literal will cause a parse error.   See: link

So I removed all the zeros on the left, but regardless of that I would have already removed it.

This is just a note because this is not what caused the error reported in the question and deviates from the main focus.

Regarding the reason for the original code to present a time with 2 hours difference, I did not find out. I just re-scripted the script for an easier way by extracting the values with the substr() function because I thought it was unnecessary to make as many calls as date() , timezone , etc.

The important thing is that the change causes the function to return what it wants.

function dataEmPortugues($date, $hours = true)
{

    $dia_num = date('w', strtotime($date));// Dia da semana.

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

    // Faz um cast integer para remover zeros a esquerda
    $mes_num = (int)substr($date, 5, 2);// Nome do mês

    if($mes_num == 1) {
        $mes_nome = 'Janeiro';
    } elseif ($mes_num == 2) {
        $mes_nome = 'Fevereiro';
    } elseif ($mes_num == 3) {
        $mes_nome = 'Março';
    } elseif ($mes_num == 4) {
        $mes_nome = 'Abril';
    } elseif ($mes_num == 5) {
        $mes_nome = 'Maio';
    } elseif ($mes_num == 6) {
        $mes_nome = 'Junho';
    } elseif ($mes_num == 7) {
        $mes_nome = 'Julho';
    } elseif ($mes_num == 8) {
        $mes_nome = 'Agosto';
    } elseif ($mes_num == 9) {
        $mes_nome = 'Setembro';
    } elseif ($mes_num == 10) {
        $mes_nome = 'Outubro';
    } elseif ($mes_num == 11) {
        $mes_nome = 'Novembro';
    } else {
        $mes_nome = 'Dezembro';
    }

    return $dia_nome.', '.(int)substr($date, 8, 2).' de '.$mes_nome.' de '.substr($date, 0, 4).($hours? ' às '.substr($date, 11, 5): '');
}

echo dataEmPortugues('2016-11-30 15:12:09');

Runtime: 0.0000169277191162109s (0.0169 milliseconds)

    
30.11.2016 / 19:10
2


Rethinking the code:

If you use the runtime version, date_default_timezone_set , call it BEFORE using the date functions. At the beginning of the code that calls the function, not inside it.

In your code you're apparently using the middle of the function, and for consistency, you'd have to use it once just after opening PHP.


Configuration alternatives:

Who defines PHP's time zone is this function:

bool date_default_timezone_set ( string $timezone_identifier )

Or this configuration directive:

date.timezone =

Manual:

  

link       link

Note: If in any situation you need the time without the spindle, you can opt for the gmdate() function, which uses the same parameters, but takes UTC as the base.


Choosing the correct string :

Below the manual links to make it easier for anyone to customize the calls to a specific region.

For Brazil, you can use:

date_default_timezone_set('America/Sao_Paulo');

And for Portugal:

date_default_timezone_set('Europe/Lisbon');

See the full list here:

  

link

    
30.11.2016 / 18:18