user3383675
user3383675

Reputation: 1081

DateTime() works wrong

There is situation in PHP Symfony2:

$myDate = new \DateTime();
var_dump($myDate);

Returns:

class DateTime#17476 (3) {
  public $date =>
  string(19) "2014-05-26 14:44:53"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Warsaw"
}

But:

$myDate = new \DateTime();
var_dump($myDate->date);

Returns... NULL

What am I doing wrong?

Upvotes: 4

Views: 108

Answers (1)

user1978142
user1978142

Reputation: 7948

Mainly because you're using it wrong, you need to properly use DateTime methods.

In this case, use ->format(). For more information please read the manual. Consider this example:

$myDate = new \DateTime();
// yyyy-mm-dd hh:mm:ss
echo $myDate->format('Y-m-d H:i:s'); // output: 2014-05-26 20:54:21
// timestamp
echo $myDate->getTimestamp(); // output: 1401108861

Upvotes: 6

Related Questions