data conversion problems with php

Asked

Viewed 103 times

0

I have the following function that transforms timestamp into English readable date

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 Stamp team

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

is returning with 2 hours difference. how do I fix it ?

  • 1

    try with gmdate and see what happens

  • same 2 hours difference

  • Actually it’s simple, need to put the date_default_timezone_set('America/Sao_Paulo'); as soon as the <?php, pq your timestamp is coming from outside. If you have a DB connection include, and/or some config used in general, you can take it and leave it there. Remove that from the function, because it will do no good to change in.

2 answers

2


Rethinking the code:

In case 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, and not inside it.

In your code apparently you are using in the middle of the function, and to have consistency, would have to use only once after opening PHP.


Configuration alternatives:

Who defines the PHP time zone is this function:

bool date_default_timezone_set ( string $timezone_identifier )

Or this configuration directive:

date.timezone =

Handbook:

http://php.net/manual/en/function.date-default-timezone-set.php http://php.net/manual/en/datetime.configuration.php#ini.date.Timezone

Note: if in any situation you need the time without the zone, you can choose the function gmdate(), which uses the same parameters, but takes UTC as the basis.


Choosing the string correct:

Below, the manual links to make it easy for anyone to customize 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:

http://php.net/manual/en/timezones.php

  • yes I’m already using this function see in my script have default Timezone inside it

  • @Jasarorion try switching to the location indicated in the update

  • did as you requested and still continues with 2 hours difference. Is my apache wrong or my php or even Mysql?

  • Weird. You have access to php.ini? you need to see how it is there. And see if the timezones are ok in PHP, a look at the apache log might help. I’m still gonna reread everything and see if I can think of any more possibilities.

  • @Jasarorion is actually simple, need to put the date_default_timezone_set('America/Sao_Paulo'); as soon as the <?php, pq your timestamp is coming from outside. If you have a DB include, and class, you can take it and leave it there. Remove that from the function, because it will do no good to change in.

  • tendi , I made a date in php and returns up to the exact somenet seconds when I pick up the db timestamp and try to convert which gets 2 hours lagged

  • You will see that the DB was saved lagged. The DB field is date or is timestamp? If everything is defaulted, with a general tidy update. You need to see if you have not missed the date_default_timezone in the file you save to DB.

Show 2 more comments

2


I took the liberty of "tweaking" the code pattern and removing redundancies. But I kept the structure so it wasn’t too invasive.

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

Before, literal octals containing invalid numbers were silently truncated (0128 would become 012). Now, an octal invalid literal will cause a parse error. Consult: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.integers

So I removed all the zeroes on the left, but I would have removed them anyway.

This is just an observation because it was not that that caused the error reported in the question and deviates a little from the main focus.

As to why the original code was two hours apart, I didn’t find out. Just reread the script for an easier way by extracting the values with the function substr() because I thought it was unnecessary to make so many calls to function 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');

Running time: 0.0000169277191162109s (0.0169 milliseconds)

Browser other questions tagged

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