DateTime Confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

DateTime Confusion

Post 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:
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: DateTime Confusion

Post 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
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: DateTime Confusion

Post 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>";
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: DateTime Confusion

Post by superdezign »

Your issue is very strange... Think it's an issue with Microsoft's DateTime object?
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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 
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: DateTime Confusion

Post 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
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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? :(
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: DateTime Confusion

Post 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$ 
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: DateTime Confusion

Post by Weirdan »

Bigun wrote:Wonderful... now what do I do? :(
Don't use $obj->date property, call $obj->format() instead.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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?
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: DateTime Confusion

Post 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.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: DateTime Confusion

Post 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
Post Reply