Checking the time difference between two PHP DateTime objects

I am checking the difference between the two timestamps returned from the database. Here is the code I use for this:

$startDate = new \DateTime($last14[$item]->started);
$endDate = new \DateTime($last14[$item]->finished);
$interval = $startDate->diff($endDate);

This returns a DateInterval object, and rightly so.

Now the problem is that I need to count all occurrences of the difference for more than 13 hours. 13 hours of the dead is normal, but 13 hours and 1 minutes need to be counted. I created a messy nested if the computer:

if ($interval->h > 12) {
    if ($interval->i > 0) {
        $count++;
    } else {
        if ($interval->h > 13) {
            $count++;
        }
    }   
}

This seems to work, but I was wondering if there is a better (cleaner) way to do this. I decided to convert the time difference into minutes and just do:

if ($minutes > $minutesIn13Hours) {
    $count++;
}

but this is a different conversion step.

Any other ideas?

+4
source share
4 answers
if ($interval->h * 60 + $interval->i > 13*60) {
    $count++;
}
+3

, - :

if ( ($interval->h * 100) + $interval->i > 1300 ) {
            $count++;
}
0

, , , , , , :

if (($interval->h > 12 && $interval->i > 0) || $interval->h > 13) {
  $count++;
}

, , , ( ).

P.S.: , @RST , , , , , ...

0

13 , .

if($endDate > $startDate->add(new DateInterval('PT13H'))) {
    $count++; // got it
}

, , . , ( ), diff, .

0

All Articles