TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   fatal error (http://www.talkphp.com/advanced-php-programming/5211-fatal-error.html)

oshash 01-06-2010 10:44 AM

fatal error
 
Hi,


Can any one tell me why i get this error::
Fatal error: Cannot redeclare date_diff() in C:\wamp\www\kamsar012010_local\includes\functions. php on line 26
And my function.php is--->

PHP Code:

<?php
// PHP USEFUL FUNCTIONS:-


function date_diff($date1$date2) {
  
//$date1  today, or any other day
  //$date2  date to check against

    
$d1 explode("-"$date1);
    
$y1 $d1[0];
    
$m1 $d1[1];
    
$d1 $d1[2];

    
$d2 explode("-"$date2);
    
$y2 $d2[0];
    
$m2 $d2[1];
    
$d2 $d2[2];

    
$date1_set mktime(0,0,0$m1$d1$y1);
    
$date2_set mktime(0,0,0$m2$d2$y2);

    return(
round(($date2_set-$date1_set)/(60*60*24)));
}
//*********LINE 26******Showing error here





function get_ip_address() {
    if (isset(
$_SERVER)) {
      if (isset(
$_SERVER['HTTP_X_FORWARDED_FOR'])) {
        
$ip $_SERVER['HTTP_X_FORWARDED_FOR'];
      } elseif (isset(
$_SERVER['HTTP_CLIENT_IP'])) {
        
$ip $_SERVER['HTTP_CLIENT_IP'];
      } else {
        
$ip $_SERVER['REMOTE_ADDR'];
      }
    } else {
      if (
getenv('HTTP_X_FORWARDED_FOR')) {
        
$ip getenv('HTTP_X_FORWARDED_FOR');
      } elseif (
getenv('HTTP_CLIENT_IP')) {
        
$ip getenv('HTTP_CLIENT_IP');
      } else {
        
$ip getenv('REMOTE_ADDR');
      }
    }

 return 
$ip;
 }




function 
is_leap_year($year) {
    if (
$year 100 == 0) {
      if (
$year 400 == 0) return true;
    } else {
      if ((
$year 4) == 0) return true;
    }

    return 
false;
  }



function 
check_email_address($email) {

  if (!
ereg("[^@]{1,64}@[^@]{1,255}"$email)) {

    return 
false;
  }

  
$email_array explode("@"$email);
  
$local_array explode("."$email_array[0]);
  for (
$i 0$i sizeof($local_array); $i++) {
     if (!
ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~.-]{0,63})|('[^(\|)]{0,62}'))$"$local_array[$i])) {
      return 
false;
    }
  }  
  if (!
ereg("^[?[0-9.]+]?$"$email_array[1])) { 
    
$domain_array explode("."$email_array[1]);
    if (
sizeof($domain_array) < 2) {
        return 
false
    }
    for (
$i 0$i sizeof($domain_array); $i++) {
      if (!
ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$"$domain_array[$i])) {
        return 
false;
      }
    }
  }
  return 
true;
}



// $start - From which year to start
// $years - number of years from the start year
function drop_down_years($start$years$direction) {
    if(
$direction==1) {
        
$end $start $years-1;
        for(
$i=$start;$i<=$end;$i++) {
            
$values[] = $i;
        }    
    }
    else {
    
$end $start $years;
    for(
$i=$end;$i<=$start;$i++) {
            
$values[] = $i;
        }    
        
$values array_reverse($values);
    }
    return (
$values);
}






function 
GetAge($Birthdate)

{
       
// Explode the date into meaningful variables
        
list($BirthYear,$BirthMonth,$BirthDay) = explode("-"$Birthdate);
        
// Find the differences
        
$YearDiff date("Y") - $BirthYear;
        
$MonthDiff date("m") - $BirthMonth;
        
$DayDiff date("d") - $BirthDay;
        
// If the birthday has not occured this year
        
if ($DayDiff || $MonthDiff 0)
          
$YearDiff--;
       return 
$YearDiff;
}

?>


etoolbox 01-06-2010 11:23 AM

You are getting that message because there's already a function called date_diff which has been declared. If you get this message, it means that

1) you have included code more than once which contains the same function,
2) have written code in more than one place which has the same function name, or
3) are trying to create a function which has the same name as a PHP function

In this instance it's #3:
http://nz.php.net/date_diff

oshash 01-06-2010 11:51 AM

hmm... I have used the same include directory in admin folder also .
is that the problem???

etoolbox 01-06-2010 11:55 AM

No, the problem is the function has already been declared because it's a built-in PHP function. Either use the PHP function and remove your function with the same name, or name your one something different.

oshash 01-06-2010 12:03 PM

how should i do that ??
am a beginner in php:(

etoolbox 01-06-2010 12:04 PM

Change
function date_diff($date1, $date2) {
to
function date_diff_some_other_name_that_doesnt_clash_with_t he_built_in_function_of_the_same_name($date1, $date2) {

oshash 01-06-2010 12:11 PM

Thanks Dude the error has gone..:)


All times are GMT. The time now is 03:22 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0