DateTime()工作错误


DateTime() works wrong

PHP Symfony2:

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

的回报:

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"
}

但:

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

返回……NULL

我做错了什么?

主要是因为使用错误,所以需要正确使用DateTime方法。

在本例中,使用 ->format() 。更多信息请阅读手册。考虑这个例子:

$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
相关文章: