zencart Fatal error: Cannot redeclare date_diff()
Zen Cart是一个比较流行的商城,尤其适合做外贸朋友,但当我们把PHP环境升级到PHP5.3的时候,Zen Cart就无法运行,并出现以下错误了:
Fatal error: Cannot redeclare date_diff() in /zencart/includes/functions/functions_general.php on line 1481
原因是在这个文件中已存在函数:date_diff()了,此时,我们需要做到与旧版本的PHP兼容,将Zen Cart中的date_diff()函数进行修改,解决方法如下:
if(!function_exists(‘date_diff’) && phpversion() >= 5.3){ //一定要注意条件中的!
date_diff(date_create($date1), date_create($date2));
}else{
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)));
}
}
替换掉如下的函数定义:
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)));
}
在 上面,我们对date_diff()函数是否存在进行判断,并判断PHP版本号,另外,也可以看到我们使用了PHP新版本的函 数:date_create(),根据给出的日期时间参数返回 Unix 时间戳,这样即可解决zencart Fatal error: Cannot redeclare date_diff() 报错的问题, 详细资料可以参看:http://www.php.net/manual/en/function.date-create.php。