减去两个日期时间并获得秒数

时间:2015-02-03 19:10:13

标签: php datetime

我尝试减去两个日期时间,以便按秒获得结果:

$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

1 个答案:

答案 0 :(得分:3)

非常浪费的代码。将时间戳格式化为字符串时只有 NO 点,只需要将它们重新设置为时间戳格式:

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

$diff_in_seconds = $now - $newdate_created;