Link to home
Start Free TrialLog in
Avatar of bootneck2222
bootneck2222

asked on

PHP date_format upgrade problem

Hi,

I am transfering an application from one server to another (Windows) and in the process moving from PHP 4.3.9 to 5.3.6. I have a script which displays stats in a table format, however I'm have problems with two functions that use date_format. Please see the attached code.

When this code is run the following error is displayed:

Fatal error: Cannot redeclare date_format() in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 4

So I have determined that I can't use "date_format" as a function name. When I change the name to dateFormat in the first function:

function dateFormat($field, $fmt)
{
      return (isSet($fmt)? "dateFormat($field, '$fmt')" : $field);
}

The following is displayed:

Warning: date_format() expects parameter 1 to be DateTime, string given in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 14 Warning: date_format() expects parameter 1 to be DateTime, string given in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 15 Warning: date_format() expects parameter 1 to be DateTime, string given in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 16

FUNCTION 'db_query': select as dt, as sh, count(*) as ct from booking_pool where 1 and >subdate(curdate(), interval 14 day)group by - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as dt, as sh, count(*) as ct from booking_pool where 1 and >subdate(curdate(),' at line 1

As the second function is calling the first:

$group = date_format($details['FIELD'], $group_fmt);

If I change this the read:

$group = dateFormat($details['FIELD'], $group_fmt);

The following is displayed:

FUNCTION 'db_query': select dateFormat(timestamp, '%Y-%m-%d') as dt, dateFormat(timestamp, '%a
%d
%b') as sh, count(*) as ct from booking_pool where 1 and dateFormat(timestamp, '%Y-%m-%d') >subdate(curdate(), interval 14 day)group by dateFormat(timestamp, '%Y-%m-%d') - FUNCTION cpark.dateFormat does not exist

Any help would be greatly appreciated.
function date_format($field, $fmt)
{
	return (isSet($fmt)? "date_format($field, '$fmt')" : $field);
}

// Do various counts
function do_counts($sql_counts, $group_fmt, $show_fmt, $where_fmt, $criteria)
{
	$stats = array();

	// Do SQL Counts
	foreach ( $sql_counts as $tag => $details ) {
		if ( isSet($details['SQL']) ) {
			$group = date_format($details['FIELD'], $group_fmt);
			$show = date_format($details['FIELD'], $show_fmt);
			$where = date_format($details['FIELD'], $where_fmt);

			$sql_query = "select $group as dt, $show as sh, count(*) as ct from " . $details['SQL'] . " " . 
				(isSet($criteria)? "and $where $criteria": "") . "group by $group";
			$result = db_query($sql_query);
			while($row = mysql_fetch_array($result))
			{
				$stats[$row['dt']]['sh'] = $row['sh'];
				$stats[$row['dt']][$tag] = $row['ct'];
			}
		}
	}

	// Do any derived functions
	foreach ( $sql_counts as $tag => $details ) {
		if ( isSet($details['FUNC']) ) {
			$func = $details['FUNC'];
			$stats = $func($stats);
		}
	}

	return $stats;
}

Open in new window

Avatar of honestman31
honestman31

Fatal error: Cannot redeclare date_format() in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 4

It means the function is already declared  before , so all what you have to do is not to declare it again .
Remove your function and how it will go
Avatar of Beverley Portlock
date-format is an existing PHP function  http://uk3.php.net/date_format

Call yours something else
Avatar of bootneck2222

ASKER

Hi,

When I remove the function the following is displayed:

Warning: date_format() expects parameter 1 to be DateTime, string given in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 14Warning: date_format() expects parameter 1 to be DateTime, string given in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 15 Warning: date_format() expects parameter 1 to be DateTime, string given in C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on line 16

FUNCTION 'db_query': select as dt, as sh, count(*) as ct from booking_pool where 1 and >subdate(curdate(), interval 14 day)group by - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as dt, as sh, count(*) as ct from booking_pool where 1 and >subdate(curdate(),' at line 1
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray said: "My guess is that the MISSING things are undefined variables"

Yep - I agree and I probably should have pointed those out as well. They come from these lines

                  $group = my_date_format($details['FIELD'], $group_fmt);
                  $show = my_date_format($details['FIELD'], $show_fmt);
                  $where = my_date_format($details['FIELD'], $where_fmt);

which, because they fail, replace $group, $show and $where with blanks or "" and thus knacker the query. Fixing the faulty function call will hopefully populate these fields with values thus resurrecting the query.

I totally endorse Ray's comment about the error_reporting levels. It is very, very useful to turn this on (it's off by default) when developing code. Add this line (below) to the start of the script

ini_set('display_errors', 1);  error_reporting( E_ALL );

Sidebar note... In the olden days, some PHP programmers may have depended on the way PHP would handle undefined variables, and on the omissions of Notices.  While computer scientists would tell you that such dependencies are terrible programming practices (and they are right) the dependencies may still be baked into your code.  Of course, nobody would do such a thing today!  In other words, you may get a lot of Notice messages and some of the Notice messages may affect the way your script works.  Example: If you use an undefined variable in a setcookie() command, or before session_start(), the PHP interpreter will send the Notice to the browser output stream.  Because it is a law of HTTP that all headers must come first and be complete before any browser output starts, setcookie() will fail.  Be on the lookout for that sort of thing.

Best of luck with the upgrade, ~Ray
Thanks guys, for help and advice