Scooter Daraf
Scooter Daraf

Reputation: 535

Substract two datetimes and get seconds

I tried to substract two datetimes to get result by seconds like that:

$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 ;

But i'm getting this error:

date_diff() expects parameter 1 to be DateTime i dont know where is the problem to getting this working

I'm looking the output to be like that :

5259000   // something like that by seconds

Upvotes: 0

Views: 63

Answers (1)

Marc B
Marc B

Reputation: 360742

Highly wasteful code. There is NO point in formatting your timestamps into strings, just to have to yank them back into timestamp format:

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

$diff_in_seconds = $now - $newdate_created;

Upvotes: 3

Related Questions