在一个非对象上调用一个成员函数Add()


Add 1 hour to current DateTime in PHP - Call to a member function add() on a non-object

我已经尝试了多个例子/问题,到目前为止,我不能得到这个工作。我试图采取当前的DateTime,增加1小时,然后将分钟设置为30。以下是我的文件:

当前时间:

$date = date('Y-m-d h:i:s'); //this works and echos properly

添加时间:

$date->add(new DateInterval("PT1H"));

我收到以下错误

在非对象上调用成员函数add()

一旦完成,我打算添加:

$date->format('Y-m-d h:30:00'); //commented out on my script until I get the above piece working.

date()产生需要使用DateTime::add()DateTime()对象。变化:

$date = date('Y-m-d h:i:s'); 

$date = new DateTime();

试着做:

$date = new DateTime('NOW');
$date->add(new DateInterval("PT1H"));