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

bootneck2222Asked:
Who is Participating?
 
Beverley PortlockCommented:
If you follow the link http://www.php.net/date_format you will see that the function in question takes a DateTime object as its parameter. Somewhere in your PHP code (and C:\Inetpub\wwwroot\cpark\reports\transaction_stats.php on lines 14, 15 and 16 look like the culprits) your are calling date_format and the parameter type is wrong.

Like I said, you need to RENAME your function like so

function my_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 = my_date_format($details['FIELD'], $group_fmt);
			$show = my_date_format($details['FIELD'], $show_fmt);
			$where = my_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



As for the SQL, it is missing a column name (I have marked it below with the word MISSING in bold

select as dt, as sh, count(*) as ct from booking_pool where 1 and MISSING >subdate(curdate(), interval 14 day)group by..... etc

And the group by needs a space between it and the bracket

0
 
honestman31Commented:
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
0
 
Beverley PortlockCommented:
date-format is an existing PHP function  http://uk3.php.net/date_format

Call yours something else
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bootneck2222Author Commented:
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
0
 
Ray PaseurCommented:
Please have a look at this article.  It should help you understand some of the DATETIME issues.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

In addition to what Brian showed in the SELECT query, you have other MISSING things.
select MISSING as dt, MISSING as sh, count(*) as ct from booking_pool

If you want to debug a PHP script, you probably want to start by adding error_reporting(E_ALL); to the top of all of your scripts.  My guess is that the MISSING things are undefined variables.  For better or worse, PHP will silently ignore the omitted variables, adding in its own context-sensitive assumptions using zero, NULL, FALSE, etc. in place of the values you think you are using.  You can get a Notice out of PHP with the right error_reporting() levels.  This will make your life a lot easier, trust me!
0
 
Beverley PortlockCommented:
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 );

0
 
Ray PaseurCommented:
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
0
 
bootneck2222Author Commented:
Thanks guys, for help and advice
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.