JohnB
JohnB

Reputation: 189

strange (?) behaviour with PHP DateTime object

I am trying to run the following

$d = "2015-Mar-22";
$dsd = DateTime::createFromFormat('Y-M-d H:i:s', $d . " 00:00:00");
$ds = strtotime($dsd->date);

and get

PHP Notice:  Undefined property: DateTime::$date in php shell code on line 1

However, if I call var_dump($dsd) before converting to a unix timestamp I get the correct answer:

echo strtotime($dsd->date);

1427007600

Am I missing something here?

Upvotes: 0

Views: 217

Answers (1)

user3942918
user3942918

Reputation: 26375

You're attempting to use an undocumented property that is only ever made available due to a side effect.

->date being available is actually a side-effect of support for var_dump()


If you just want a timestamp use $dsd->getTimestamp() instead of strtotime($dsd->date).

Upvotes: 2

Related Questions