SomeBeginner
SomeBeginner

Reputation: 49

How to check if the difference between 2 dates is bigger than 15 minutes?

I am using this function to compare a date from the database to the current date, and i need to check if the difference between the 2 dates is bigger than 15 minutes but i don't know how to do that, i think i need to do something like if($comp > 0 days 0 hours 15 minutes)

function TimeOut($dateP){
$date = new DateTime(date('Y-m-d H:i:s'));
$date2 =  new DateTime($dateP);

   echo $comp = $date->diff($date2)->format("%d days %h hours and %i minuts %s seconds");
    if ($comp > "15 minutes ?") {
    return true;
 }
}

Upvotes: 0

Views: 1042

Answers (1)

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

You can use diff and then read the m parameter of the result. In the example below $difference will be DateInterval object:

$difference = $start_date->diff($date2);

if($difference->i > 15) {
    echo "difference greater than 15 minutes"
}

A date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTime's constructor supports.

Upvotes: 2

Related Questions