php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52535 DateTime::createFromFormat doesn't return a valid DateTime Object?
Submitted: 2010-08-04 21:34 UTC Modified: 2010-08-04 23:18 UTC
From: webdeveloper02 at yahoo dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.3 OS: linux 32/64-bit, os-x
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: webdeveloper02 at yahoo dot com
New email:
PHP Version: OS:

 

 [2010-08-04 21:34 UTC] 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

#

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-04 21:40 UTC] 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 22:00 UTC] rasmus@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: derick
 [2010-08-04 22:09 UTC] 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 23:18 UTC] webdeveloper02 at yahoo dot com
-Status: Verified +Status: Closed
 [2010-08-04 23:18 UTC] webdeveloper02 at yahoo dot com
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Aug 11 17:00:03 2025 UTC