114

Using the DateTime class, if I try to run the following code:

$mydate = new DateTime();
echo $mydate->date;

I'll get back this error message

Notice: Undefined property: DateTime::$date...

Which doesn't make sense because when running var_dump() on the variable $mydate, it clearly shows that this property exists and is publicly accessible:

var_dump($mydate);

object(DateTime)[1]
  public 'date' => string '2012-12-29 17:19:25' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

Is this a bug within PHP or am I doing something wrong? I'm using PHP 5.4.3.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
okey_on
  • 2,888
  • 8
  • 28
  • 36

5 Answers5

171

This is a known issue.

Date being available is actually a side-effect of support for var_dump() here – derick@php.net

For some reason, you're not supposed to be able to access the property but var_dump shows it anyways. If you really want to get the date in that format, use the DateTime::format() function.

echo $mydate->format('Y-m-d H:i:s');
jeremy
  • 9,965
  • 4
  • 39
  • 59
19

Update: The behaviour has changed in PHP7.3, the original answer doesn't work anymore. To get the same results with all PHP versions, incl. >=7.3, you can use the following code:

$dt = new DateTime();
$date = $dt->format('Y-m-d\TH:i:s.v');

For the record, the original answer:

Besides calling DateTime::format() you can access the property using reflection:

<?php

$dt = new DateTime();
$o = new ReflectionObject($dt);
$p = $o->getProperty('date');
$date = $p->getValue($dt);

This is slightly faster than using format() because format() formats a timestring that has already been formatted. Especially if you do it many times in a loop.

However this is not a documented behaviour of PHP, it may change at any time.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • How this stopped working in PHP 7.4? 7.2 version http://sandbox.onlinephpfunctions.com/code/38d86c07d4c50ee12b8c0a47e82ef6ffc0327592 7.4 - Error http://sandbox.onlinephpfunctions.com/code/38d86c07d4c50ee12b8c0a47e82ef6ffc0327592 – TechCare99 Nov 18 '20 at 15:27
  • 1
    @TechCare99 :) I think it should never worked like this, it was a hack. Need to check 7.4 for an alternative, not doing much php nowadays but will check it. Thanks for letting me know! – hek2mgl Nov 18 '20 at 15:28
  • Updated to work with [all PHP versions](https://3v4l.org/3tc31) – hek2mgl Nov 22 '20 at 22:38
4

The date property of DateTime is protected.

You can display the date with format function.

<?php

try {
    $time = new DateTime();
    echo($time->format("Y-m-d H:i:s"));
} catch (Exception $e) {
}

Or you can convert to array:

<?php

try {
    $time = (array) new DateTime();
    var_dump($time["date"]);
} catch (Exception $e) {
}
filipe
  • 1,957
  • 1
  • 10
  • 23
  • He wanted to know why couldn't he access the date property of DateTime object... Where do you see he wanted to format a stored Datetime values ? "Y-m-d H:i:s" is the format in witch the date is by default ! – filipe Oct 12 '19 at 12:41
  • Object to array conversion - as you suggest - is probably the easiest way to go. – Jeffz May 21 '20 at 12:00
3

As noted by the other answers, it is an issue with PHP which is unresolved as of today but if it is a 'side effect' of var_dump() I am not so sure..

echo ((array) new DateTime())['date']; // Works in PHP 7.

What I am sure about is that if the properties of DateTime where meant to be used by us it would have been made available. But like many internal classes they are not and you shouldn't rely on "hacky" or "glitchy" methods to fix your code. Instead you should use their API.

echo (new DateTime())->format('Y-m-d H:i:s');

If you are not satisfied you can extend the class or perhaps use Carbon that extends it for you.

echo (new Carbon())->toDateTimeString();

If you wounder how var_dump() creates a fake output of an object take a look at __debugInfo()

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
-2

If you just use a var_Dump before ask the property date everything works allright:

$mydate = new DateTime();
var_Dump($mydate);
echo '<br>';
echo $mydate->date;

This delivers:

object(DateTime)#1 (3) { ["date"]=> string(26) "2017-04-11 08:44:54.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" }
2017-04-11 08:44:54.000000

So you see the property date exists even for the object. I can't understand this behaviour. Just comment out the var_Dump and you will get the error again.

Sascha
  • 4,576
  • 3
  • 13
  • 34