Php 减去两个datetimes,得到秒数

Php 减去两个datetimes,得到秒数,php,datetime,Php,Datetime,我试着用两个datetimes减法,以秒为单位得到结果,如下所示: $created = "2015-01-16 07:26:55"; $newdate_created = date('Y-m-d H:i:s', strtotime('+2 month', strtotime($created))); $now = date('Y-m-d h:i:s'); $interval = date_diff($now, $newdate_created); $seconds = $interval *

我试着用两个datetimes减法,以秒为单位得到结果,如下所示:

$created = "2015-01-16 07:26:55";
$newdate_created = date('Y-m-d H:i:s', strtotime('+2 month', strtotime($created)));
$now = date('Y-m-d h:i:s');
$interval = date_diff($now, $newdate_created);
$seconds = $interval * 60 * 60 * 12 ;
但我得到了一个错误:

date_diff()要求参数1为DateTime 我不知道让它工作的问题在哪里

我希望输出是这样的:

5259000   // something like that by seconds

非常浪费的代码。将时间戳格式化为字符串是没有意义的,只需将它们拉回时间戳格式即可:

$newdate_created = strtotime('+2 month', strtotime($created));
$now = time();

$diff_in_seconds = $now - $newdate_created;

非常浪费的代码。将时间戳格式化为字符串是没有意义的,只需将它们拉回时间戳格式即可:

$newdate_created = strtotime('+2 month', strtotime($created));
$now = time();

$diff_in_seconds = $now - $newdate_created;

非常浪费的代码。将时间戳格式化为字符串是没有意义的,只需将它们拉回时间戳格式即可:

$newdate_created = strtotime('+2 month', strtotime($created));
$now = time();

$diff_in_seconds = $now - $newdate_created;

非常浪费的代码。将时间戳格式化为字符串是没有意义的,只需将它们拉回时间戳格式即可:

$newdate_created = strtotime('+2 month', strtotime($created));
$now = time();

$diff_in_seconds = $now - $newdate_created;

date()
$newdate_created=newdatetime(“+2个月”)。然后您的
$interval
将是一个
DateInterval
对象。
date()
不会创建使用
date_diff()
所需的DateTime对象。是的,如果您坚持使用此代码,它应该是
$now=new DateTime()
$newdate_created=newdatetime(“+2个月”)。然后您的
$interval
将是一个
DateInterval
对象。
date()
不会创建使用
date_diff()
所需的DateTime对象。是的,如果您坚持使用此代码,它应该是
$now=new DateTime()
$newdate_created=newdatetime(“+2个月”)。然后您的
$interval
将是一个
DateInterval
对象。
date()
不会创建使用
date_diff()
所需的DateTime对象。是的,如果您坚持使用此代码,它应该是
$now=new DateTime()
$newdate_created=newdatetime(“+2个月”)。然后你的
$interval
将是一个
DateInterval
对象。它真的是那么简单