将 PHP 中的当前 DateTime 添加 1 小时 - 调用非对象上的成员函数 add()
Add 1 hour to current DateTime in PHP - Call to a member function add() on a non-object
我已经尝试了多次 examples/questions,但到目前为止我无法让它工作。我正在尝试使用当前 DateTime
,加上 1 小时,然后将分钟设置为 30
。这是我拥有的:
当前时间:
$date = date('Y-m-d h:i:s'); //this works and echos properly
增加时间:
$date->add(new DateInterval("PT1H"));
我收到以下错误
Call to a member function add() on a non-object
完成后我打算添加:
$date->format('Y-m-d h:30:00'); //commented out on my script until I get the above piece working.
date()
而不是 会生成一个 DateTime()
对象,该对象需要使用 DateTime::add()
。变化:
$date = date('Y-m-d h:i:s');
至
$date = new DateTime();
尝试做:
$date = new DateTime('NOW');
$date->add(new DateInterval("PT1H"));
我已经尝试了多次 examples/questions,但到目前为止我无法让它工作。我正在尝试使用当前 DateTime
,加上 1 小时,然后将分钟设置为 30
。这是我拥有的:
当前时间:
$date = date('Y-m-d h:i:s'); //this works and echos properly
增加时间:
$date->add(new DateInterval("PT1H"));
我收到以下错误
Call to a member function add() on a non-object
完成后我打算添加:
$date->format('Y-m-d h:30:00'); //commented out on my script until I get the above piece working.
date()
而不是 会生成一个 DateTime()
对象,该对象需要使用 DateTime::add()
。变化:
$date = date('Y-m-d h:i:s');
至
$date = new DateTime();
尝试做:
$date = new DateTime('NOW');
$date->add(new DateInterval("PT1H"));