Error "Fatal error: Cannot redeclare (Previously declared)"

Asked

Viewed 4,248 times

1

I have a notification button, where it shows the last requests that will come out in the system, I wanted to add the time that he has already left. Ex.: 4 minutes ago, 1 hour ago, and when it was above 24 hours it showed 1 days ago, 2 days ago.... I tried several ways to shorten the hours, but could not. In my database I save the following data:

data_record -> the day it was registered Ex.: 2017-05-18;

horario_record- > the time that was registered Ex.: 10:19:30

function tempoCorridoDois($dataHoraString2) {
    $hoje2 = strtotime(date('Y/m/d H:i:s'));
    $dataHora2 = strtotime($dataHoraString2);
    $diferenca2 = $hoje2 - $dataHora2;

    $segundos2 = $diferenca2;
    $minutos2 = round($diferenca2 / 60);
    $horas2 = round($diferenca2 / 3600);
    $dias2 = round($diferenca2 / 86400);
    $semanas2 = round($diferenca2 / 604800);
    $meses2 = round($diferenca2 / 2419200);
    $anos2 = round($diferenca2 / 29030400);

    if ($segundos2 <= 60) {
      return "1 minuto atrás.";
    }else if ($minutos2 <= 60) {
      return  $minutos2 . ' minutos atrás.';
      }else if ($horas2 <= 24) {
        return $horas2 . ' horas atrás.';
        }else if ($dias2 <= 7) {
          return $dias2 . ' dia(s) atrás.';
          }else if ($semanas2 <= 4) {
            return $semanas2 . ' semanas atrás.';
            }else if ($meses2 <= 12) {
              return $meses2 . ' meses atrás.';
              }else{
                return $anos2 . ' anos atrás.';
              }
  }
      $tempoFinal2 = tempoCorridoDois($dataregistro2.$horarioregistro2);

And the following error appears:
Fatal error: Cannot redeclare timeCorridoDois() (Previously declared in C: xampp htdocs php component requests ff_notificacao_php.php:136) in C: xampp htdocs php component requests ff_notificacao_php.php on line 136

  • You are saving the date in the bank in this format?

  • Already fixed @Lucasthibaupaulino

  • Well, you can use date_diff() (ex: https://www.w3schools.com/php/func_date_date_diff.asp) or treat the date and time as a string, separating it into blocks and doing the manual calculation. Both ways work, just very careful at the time of treating this information, any little mistake makes everything stop working.

  • The edition invalidated an answer and anyway there is already a question on the subject in https://answall.com/q/83326/3635. with two answers, no need for more. If you have any extra questions besides the specific problem of comparing two dates then I recommend creating a new question.

2 answers

6

The mistake:

Fatal error: Cannot redeclare timeCorridoDois() (Previously declared in C: xampp htdocs php component requests ff_notificacao_php.php:136) in C: xampp htdocs php component requests ff_notificacao_php.php on line 136

Indicates that the function has been declared twice, either you realmenete declared twice or you must be using the include (or require) more than once in different files

To solve change require or include for require_once or include_once, if this is not the problem then review your scripts there should really be two functions with the same name.

About "humanize the hours", I recommend you try the solutions presented in:

4


You can use the date_diff function() or manually do the same as shown below:

 function tempoCorrido($dataHoraString) {
    $hoje = time();
    $dataHora = strtotime($dataHoraString);
    $diferenca = $hoje - $dataHora;

    $segundos = $diferenca;
    $minutos = round($diferenca / 60);
    $horas = round($diferenca / 3600);
    $dias = round($diferenca / 86400);
    $semanas = round($diferenca / 604800);
    $meses = round($diferenca / 2419200);
    $anos = round($diferenca / 29030400);

    if ($segundos <= 60) {
        return $segundos . " segundos atrás";
    } 
    else if ($minutos <= 60) {
        return  $minutos . 'min atrás';
    } 
    else if ($horas <= 24) {
        return $horas . ' hrs atrás';
    } 
    else if ($dias <= 7) {
        return $dias . ' dias atrás';
    } 
    else if ($semanas <= 4) {
        return $semanas . ' semanas atrás';
    } 
    else if ($meses <= 12) {
        return $meses . ' meses atrás';
    } 
    else {
        return $anos . ' anos atrás';
    }
}

echo tempoCorrido("2017-05-18 10:40:00");
  • Diego take a look at my code I’m in trouble, my question is edited.

  • 1

    You declared a function named timeCorridoDois in the ff_notificacao_php file and also declared this function in the file you are running. You can’t have two roles with the same name.

  • pq only gives as a result 1 minute?

  • 1

    Diego worked out I really appreciate

  • @Leo Caracciolo The $seconds <= 60 condition was set to display 1 minute. I set it to display in seconds.

  • 1

    Diego and @Gabrielfilippi A tip, change $hoje = strtotime(date('Y/m/d H:i:s')); for $hoje = time();, use strtotime(date('Y/m/d H:i:s')) is quite redundant

  • @Guilhermenascimento, thanks for the tip!

Show 2 more comments

Browser other questions tagged

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