cybernet2u
cybernet2u

Reputation: 168

DateTime Comparison with objects php

This may be the most simple question however i don't know to best aproach this

I have 2 dates to compare $lp & now()

$lp = $ur->getLastPost($usr) ?: new \DateTime('yesterday');
$tdiff = date_diff(strtotime($lp),strtotime('now'));

$lp can either be 2019-11-21 17:20:44 or an current yesterday time

I want to transform $lp in a unixtimestamp so I can see if the difference is bigger than 3600 seconds

My problem is that i can't use strtotime because $lp can be an object, hence the error strtotime() expects parameter 1 to be string, object given

Upvotes: 0

Views: 256

Answers (1)

ttrasn
ttrasn

Reputation: 4851

You must parse datetime to format of you $lp:

$date = new DateTime($ur->getLastPost($usr) ?: 'yesterday');
$date->format('Y-m-d H:i:s');

then you can use date_diff to get difference of dates.

print_r($date->diff(new \DateTime('now')));

see DateTime::diff and Datetime class

Upvotes: 1

Related Questions