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?
source
share