0

This is my code:

$date1 = new DateTime();
$date1->format('Y-m-d H:i:s');
echo $date1->date. ' while echoing date1';  

It echoes only " while echoing date1", in other words $date1->date is empty.
If I add dump date1 first like this:

$date1 = new DateTime();
$date1->format('Y-m-d H:i:s');
var_dump($date1);
echo $date1->date. ' while echoing date1';  

I get

object(DateTime)[359]
  public 'date' => string '2016-06-26 16:54:56.000000' (length=26)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

2016-06-26 16:54:56.000000 while echoing date1

which is what I want. This thing is getting me mad as it is completely unexpected.

Ferex
  • 553
  • 6
  • 22
  • 1
    var_dump($date1); is outputing 2016-06-26 16:54:56.000000 that's why they are different –  Jun 26 '16 at 16:56
  • `var_dump()`....:):) remove and check – Alive to die - Anant Jun 26 '16 at 16:58
  • 1
    I'm not sure if this is simply asking why this is happening or if you are trying to figure out how to use DateTime? You should be using `echo $date1->format('Y-m-d H:i:s');` not relying on the `$date` property. You'd have to look into the code of the DateTime class, but I'd assume that the `$date` property isn't filled by the constructor or format method. – Devon Bessemer Jun 26 '16 at 16:58
  • I need to know how to access the date property without doing the dump first – Ferex Jun 26 '16 at 16:58
  • http://php.net/manual/en/class.datetime.php does not seem to have `->date`, so I don't understand why in the second example the output will come out –  Jun 26 '16 at 16:59
  • @Ferex Try once more and see if that is really the case –  Jun 26 '16 at 17:02
  • @SuperCoolHandsomeGelBoy you are true, I didn't write `->date()` but `->date` which DateTime object has in it's properties – Ferex Jun 26 '16 at 17:02
  • Well, I didn't realized that, thanks for the link –  Jun 26 '16 at 17:05
  • I don't think it's worth a downvote – Ferex Jun 26 '16 at 17:36

1 Answers1

2

The format option isn't for setting the format of the object, it's a method that returns the date object as a string in the requested format:

echo $date->format('Y-m-d H:i:s');

To clarify, what you see as a 'date' within the object is just a text representation for convenience, as internally it will be held in an optimized way. If you want to add it to a string, you need to specify the format you want:

echo "This is the date ".$date->format('c');
WhoIsRich
  • 4,053
  • 1
  • 33
  • 19
  • If that is the case why would the first one not outputting? –  Jun 26 '16 at 16:59
  • 1
    @SuperCoolHandsomeGelBoy, not really a phenomenon. No where in the manual does it suggest to use the `$date` property, you'd just need to see when the `$date` property is getting set in the class. – Devon Bessemer Jun 26 '16 at 17:02
  • 1
    Yes but why I can access the property only after dumping the object?? Isn't it strange?? – Ferex Jun 26 '16 at 17:04
  • @Devon sorry I've changed the comment –  Jun 26 '16 at 17:04
  • 1
    @Ferex, take a look at http://stackoverflow.com/questions/14084222/why-cant-i-access-datetime-date-in-phps-datetime-class-is-it-a-bug. From a quick glance, the `$date` property is just provided for debugging. – Devon Bessemer Jun 26 '16 at 17:05