How to check if DateTime difference exceeds a threshold in PHP?

$dt1 = new DateTime($myObject['ModifyTime']);
$dtNow = new DateTime();
$timeDifference = date_diff($dt1,$dtNow);

I need to check if DateTime dt1 is older than now by >300 minutes. How can I check that?


You can do it purely on timestamps also:


$dt1 = new DateTime($myObject['ModifyTime']);
$now = time();

var_dump($now - $dt1->getTimestamp() >= 300 * 60); // should return true


Timestamp is represented in seconds (from 1970-01-01). So 300 minutes is 300 x 60 seconds.

The difference between timestamps is in seconds so just divide it by 60 for minutes and so on

$datetime1 = new DateTime('2014-03-12 11:00');
$datetime2 = new DateTime('2014-03-12 16:00');
// difference in seconds
$interval = datetime2->getTimestamp() - $datetime1->getTimestamp();

// difference in minutes
$interval = interval / 60;

// difference in hours
$interval = interval / (60 * 60);

Check if the difference is greater than 5 hours:

$dt1 = new DateTime($myObject['ModifyTime']);
$dtNow = new DateTime();
$timeDifference = date_diff($dt1,$dtNow);
if ($timeDifference->h > 5) {
  // greater than 300 minutes

See it in action

See DatInterval::format() for more information on DateInterval formatting

Need Your Help

Laravel 5 get view name

php templates view laravel-5

I'm struggling to get the view name in L5. Just as in WP, I'd like to add a specific page name (view name) for styling, like so:

Marshalling and unmarshalling a List<List<T>> with Android's Parcel

android marshalling unmarshalling parcelable

I get a ClassNotFoundException when trying to unmarshall a List&lt;List&lt;LatLng&gt; in a custom class which implements Parcelable. How do I get rid of this exception?