Edit report at http://bugs.php.net/bug.php?id=52535&edit=1

 ID:                 52535
 User updated by:    webdeveloper02 at yahoo dot com
 Reported by:        webdeveloper02 at yahoo dot com
 Summary:            DateTime::createFromFormat doesn't return a valid
                     DateTime Object?
-Status:             Verified
+Status:             Closed
 Type:               Bug
 Package:            Date/time related
 Operating System:   linux 32/64-bit, os-x
 PHP Version:        5.3.3
 Assigned To:        derick
 Block user comment: N

 New Comment:

Closing this out - The proper way to access the object is via the
$y->getTimestamp();  or $y->format();  methods.



It was a little confusing to be calling DateTime::createFromFormat() in
order to get a DateTime object, when the format and time values matched
and all I needed was a DateTime object that returned the value I was
already passing in to it.


Previous Comments:
------------------------------------------------------------------------
[2010-08-04 22:09:08] webdeveloper02 at yahoo dot com

Additional note, for what it's worth:



performing:



   var_dump(($y instanceof DateTime));



..after line 13 of the test script confirms the object is being
instantiated: 



   bool(true)

------------------------------------------------------------------------
[2010-08-04 21:40:53] dcousineau at gmail dot com

Just as a side note, I also ran his snippet on my 5.3.1 install (stock
OSX 

install) and received the same results.

------------------------------------------------------------------------
[2010-08-04 21:34:57] webdeveloper02 at yahoo dot com

Description:
------------
When creating a new DateTime object via DateTime::createFromFormat() the
date / timezone_type and timezone methods return: PHP Notice:  Undefined
property: ...etc.



If you var_dump() the object however and then call these methods they
suddenly work / return what is expected.



See test script for example.

Test script:
---------------
<?php

/*

  PHP 5.3.2 / 5.3.3 (cli)

*/



date_default_timezone_set('America/New_York');



$x = '2010-07-21 20:49:07';



// $tz = new DateTimeZone('America/New_York'); // broken even with out
this.

// $y = DateTime::createFromFormat('Y-m-d H:i:s', $x, $tz); // doesn't
make a difference.



$y = DateTime::createFromFormat('Y-m-d H:i:s', $x);



echo $y->date; // PHP Notice:  Undefined property: DateTime::$date



var_dump($y); // Something magical happens.



echo $y->date; // Suddenly this works.



?>

Expected result:
----------------
Would expect to see the proper output of calling $y->date on line #15
and not need to call var_dump($y) to get it to work?

Actual result:
--------------
# php tmp.php

PHP Notice:  Undefined property: DateTime::$date in /path/to/tmp.php on
line 15

PHP Stack trace:

PHP   1. {main}() /path/to/tmp.php:0

object(DateTime)#1 (3) {

  ["date"]=>

  string(19) "2010-07-21 20:49:07"

  ["timezone_type"]=>

  int(3)

  ["timezone"]=>

  string(16) "America/New_York"

}

2010-07-21 20:49:07



#


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52535&edit=1

Reply via email to