Error showing time passed from multiple posts in PHP

2
<?php
function timeAgo($time_ago){
$cur_time   = time();
$time_elapsed   = $cur_time - $time_ago;
$seconds    = $time_elapsed ;
$minutes    = round($time_elapsed / 60 );
$hours      = round($time_elapsed / 3600);
$days       = round($time_elapsed / 86400 );
$weeks      = round($time_elapsed / 604800);
$months     = round($time_elapsed / 2600640 );
$years      = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
    echo "$seconds segundos atrás";
}
//Minutes
else if($minutes <=60){
    if($minutes==1){
        echo "um minuto atrás";
    }
    else{
        echo "$minutes minutos atrás";
    }
}
//Hours
else if($hours <=24){
    if($hours==1){
        echo "uma hora atrás";
    }else{
        echo "$hours horas atrás";
    }
}
//Days
else if($days <= 7){
    if($days==1){
        echo "ontem";
    }else{
        echo "$days dias atrás";
    }
}
//Weeks
else if($weeks <= 4.3){
    if($weeks==1){
        echo "à uma semana";
    }else{
        echo "$weeks semanas atrás";
    }
}
//Months
else if($months <=12){
    if($months==1){
        echo "um mês atrás";
    }else{
        echo "$months meses atrás";
    }
}
//Years
else{
    if($years==1){
        echo "um ano atrás";
    }else{
        echo "$years anos atrás";
    }
}
}

?>
<?php
  $curenttime=$date_uploaded;
  $time_ago =strtotime($curenttime);
  echo timeAgo($time_ago);
?>

Being $date_upload is equal to (dd-mm-aa hh: mm: ss) in the database.

This is the code, when I pull only one post it works, but when I pull multiple posts it gives this error:

  

Error: Can not redeclare timeAgo () (previously declared in C: \ xampp \ htdocs \ site \ poster.php: 520))

    
asked by anonymous 25.07.2015 / 16:17

1 answer

2

The error says that there is two or more times a function with the same name in your file or in the call of an include.

To solve the problem, I suggest you have at least two files, one with normal code processing and another with only a small library function, thus avoiding the collision of function names. Another alternative from php5.3 is to use name espaces.

There are some tools that identify code duplication and copy & paste like php mess dectector , also helps in identifying these collisions.

Example - dummy error.

Example - solution with namespaces.

    
25.07.2015 / 18:00