-1
$now = new DateTime();
print_r($now);
print $now->date; // print the current date

BUT if print_r($now); is comment it show error ?

$now = new DateTime();
print $now->date; // Notice: Undefined property: DateTime::$date in
  • 1
    There is no `date` defined in the member functions/attributes [DateTime](http://php.net/manual/en/class.datetime.php) – DevZer0 Jul 24 '13 at 07:52
  • Please do not use undocumented properties or bad things will happen. – PeeHaa Jul 24 '13 at 08:02

3 Answers3

0

There is no such property in this class. Use format function instead:

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

http://www.php.net/manual/en/datetime.format.php

user4035
  • 22,508
  • 11
  • 59
  • 94
0

This is a bug in PHP (I 'm not sure exactly which versions are affected).

Class DateTime does not have a date property, but calling print_r on it creates a "hidden" property that looks like it's there (it is displayed with print_r) but in reality is not (you cannot get its value).

Instead of this, use DateTime::format to get the date value in whatever format you want.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

When you want to print the date using the DateTime object, use the following method:

$Date = new DateTime();
$Date->format('d/m/Y H:i');

The following page can help you format the output:

http://php.net/manual/en/function.date.php

Benz
  • 2,317
  • 12
  • 19