Page 1 of 2

DateTime Confusion

Posted: Tue Aug 03, 2010 2:11 pm
by Bigun
Ok, I have an object residing on an array. The array is:

Code: Select all

$row
The Array field containing the object is:

Code: Select all

$row['Date']
Doing this, spits out the contents of the object:

Code: Select all

print_r($row['Date']);
Here is where the confusion sets in... HARD.
This:

Code: Select all

$objecttest = $row['Date'];
print_r($row['Date']);
echo "--->" . $objecttest->date . "<-- Date<br>";
Outputs This:

Code: Select all

DateTime Object ( [date] => 2010-08-03 00:00:00 [timezone_type] => 3 [timezone] => America/New_York ) --->2010-08-03 00:00:00<-- Date
However, this:

Code: Select all

$objecttest = $row['Date'];
//print_r($row['Date']);
echo "--->" . $objecttest->date . "<-- Date<br>";
Outputs This:

Code: Select all

---><-- Date
I am thoroughly confused. I'm also not used to dealing with objects. All of these code changes were due to having to change MS-SQL modules for FastCGI, but that's another story.

Help?

:banghead:

Re: DateTime Confusion

Posted: Tue Aug 03, 2010 3:36 pm
by PHPHorizons
Hello Bigun

Your code is untestable because DateTime does not have a date property as far as I can tell. http://us.php.net/manual/en/class.datetime.php

Where is the date property coming from?

Secondly, commenting out that one line like you did will not have any affect on the next line of code. print_r only outputs data. It does not change any data contained in a variable.

Cheers

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 2:15 am
by internet-solution
"->" operator is used only for accessing memeber variables or functions in a class. So this is not the right use in your context and and will not work.

$objecttest is same as $row['Date'], so you can just use the following for printing it out -

Code: Select all

echo "--->" . $objecttest . "<-- Date<br>";

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 7:08 am
by Bigun
The code is a function called to display a date from a MSSQL record:

Code: Select all

<?php
function echodate($releaseid) //Echo the release's date
{
	include('./config.php');
	
	$dbhandle = sqlsrv_connect($MSSQLServerName, $DBName);
	if( $dbhandle === false ) { 
		die(FormatErrors(sqlsrv_errors())); 
	}

	$releaseid = checkreleaseid($releaseid);
	
	$query = "SELECT Date FROM Releases WHERE ReleaseID = '$releaseid'";
	
	$result = sqlsrv_query($dbhandle,$query);
	
	$row = sqlsrv_fetch_array($result);
	
	if ($row['Date'] <> "") {
		$objecttest = $row['Date'];
		//print_r($row['Date']);
		echo "--->" . $objecttest->date . "<-- Date<br>";
		$pieces = explode("-", $row['Date']->Date);
		$date = $pieces[0] . "/" . $pieces[1] . "/" . $pieces[2];
		echo $date;
	}
}
?>
The DateTime stamp is coming from a MSSQL record, and I'm using MicroSoft's SQL driver for PHP.

And again, I'm not familiar with dealing with objects.... like at all.

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 7:13 am
by superdezign
Your issue is very strange... Think it's an issue with Microsoft's DateTime object?

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 7:22 am
by Bigun
superdezign wrote:Your issue is very strange... Think it's an issue with Microsoft's DateTime object?
Possibly. But until I know how to deal with objects, I really can't say just yet.

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 7:25 am
by Bigun
internet-solution wrote:"->" operator is used only for accessing memeber variables or functions in a class. So this is not the right use in your context and and will not work.

$objecttest is same as $row['Date'], so you can just use the following for printing it out -

Code: Select all

echo "--->" . $objecttest . "<-- Date<br>";
I did what you said, this was the result:

Code: Select all

Catchable fatal error: Object of class DateTime could not be converted to string in D:\Inetpub\wwwpublic\functions.php on line 66 

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 7:28 am
by Bigun
PHPHorizons wrote:Hello Bigun

Your code is untestable because DateTime does not have a date property as far as I can tell. http://us.php.net/manual/en/class.datetime.php

Where is the date property coming from?
MSSQL field via MicroSoft's MSSQL PHP driver
PHPHorizons wrote: Secondly, commenting out that one line like you did will not have any affect on the next line of code. print_r only outputs data. It does not change any data contained in a variable.

Cheers
You saw the posted output, somehow it did. But again, it may be that I don't understand objects fully either.

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 8:14 am
by Weirdan
I can confirm this is the issue with print_r + DateTime (and not related to mssql):

Code: Select all

weirdan@weirdan:~$ php -r '$d = new DateTime; print_r($d); var_dump($d->date);'
DateTime Object
(
    [date] => 2010-08-04 15:26:52
    [timezone_type] => 3
    [timezone] => Europe/Helsinki
)
string(19) "2010-08-04 15:26:52"



weirdan@weirdan:~$ php -r '$d = new DateTime; var_dump($d->date);'
PHP Notice:  Undefined property: DateTime::$date in Command line code on line 1
NULL


weirdan@weirdan:~$ php -v
PHP 5.3.2-2 with Suhosin-Patch (cli) (built: Jul 19 2010 01:22:58) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Suhosin v0.9.31, Copyright (c) 2007-2010, by SektionEins GmbH

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 8:20 am
by Bigun
Weirdan wrote:I can confirm this is the issue with print_r + DateTime (and not related to mssql):

Code: Select all

weirdan@weirdan:~$ php -r '$d = new DateTime; print_r($d); var_dump($d->date);'
DateTime Object
(
    [date] => 2010-08-04 15:26:52
    [timezone_type] => 3
    [timezone] => Europe/Helsinki
)
string(19) "2010-08-04 15:26:52"



weirdan@weirdan:~$ php -r '$d = new DateTime; var_dump($d->date);'
PHP Notice:  Undefined property: DateTime::$date in Command line code on line 1
NULL


weirdan@weirdan:~$ php -v
PHP 5.3.2-2 with Suhosin-Patch (cli) (built: Jul 19 2010 01:22:58) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Suhosin v0.9.31, Copyright (c) 2007-2010, by SektionEins GmbH

Wonderful... now what do I do? :(

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 8:30 am
by Weirdan
It seems those properties (date, timezone_type and timezone) are created whenever the datetime object is interrogated for the list of it's properties. foreach(), property_exists(), print_r and Reflection::export() - all of those calls trigger property creation. Even array cast without saving the result triggers it:

Code: Select all

weirdan@weirdan:/home/sam/reporting$ php -r '$d = new DateTime; var_dump($d->date); (array)$d; var_dump($d->date);'
PHP Notice:  Undefined property: DateTime::$date in Command line code on line 1
NULL
string(19) "2010-08-04 16:29:22"
weirdan@weirdan:/home/sam/reporting$ 

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 8:36 am
by Weirdan
Bigun wrote:Wonderful... now what do I do? :(
Don't use $obj->date property, call $obj->format() instead.

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 8:46 am
by Bigun
Weirdan wrote:It seems those properties (date, timezone_type and timezone) are created whenever the datetime object is interrogated for the list of it's properties. foreach(), property_exists(), print_r and Reflection::export() - all of those calls trigger property creation. Even array cast without saving the result triggers it:

Code: Select all

weirdan@weirdan:/home/sam/reporting$ php -r '$d = new DateTime; var_dump($d->date); (array)$d; var_dump($d->date);'
PHP Notice:  Undefined property: DateTime::$date in Command line code on line 1
NULL
string(19) "2010-08-04 16:29:22"
weirdan@weirdan:/home/sam/reporting$ 
Awesome. That gives me the workaround that I needed. However, do I need to followup on this with the PHP team as a bug? Or is this behavior desired?

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 8:50 am
by Bigun
Weirdan wrote:
Bigun wrote:Wonderful... now what do I do? :(
Don't use $obj->date property, call $obj->format() instead.
That works even better.

Re: DateTime Confusion

Posted: Wed Aug 04, 2010 10:40 am
by PHPHorizons
Weirdan wrote:It seems those properties (date, timezone_type and timezone) are created whenever the datetime object is interrogated for the list of it's properties. foreach(), property_exists(), print_r and Reflection::export() - all of those calls trigger property creation. Even array cast without saving the result triggers it:
LOL That's what I get for relying on the manual :P. Is that stated anywhere on the PHP site; because, I have not seen anything of the sort. Nor does the DateTime manual pages say there are date, timezone_type, and timezone properties (as far as I know)

Anyways, I'm glad to know about this "gotcha".

Cheers