Reputation: 718
I have to compare if result from a subtraction is greater on less than 2 hours. I don't know how exactly do make this comparison. I tried the following. It is correct for the third if, but for the first and second result is not correct.
Here's my view:
<?php
$transport= new DateTime($row->transportDate);
$max=new DateTime(max($forecast_array));
$interval = $transport->diff($max);
if($max->format('Y-m-d H:i:s') < $transport->format('Y-m-d H:i:s') && $interval->format('%h:%i:%s') >= '2:00:00' ) {
echo '<img src="<?= base_url();?>/assets/images/tick.png">';
}
if($max->format('Y-m-d H:i:s') < $transport->format('Y-m-d H:i:s') && $interval->format('%h:%i:%s') < '2:00:00' ) {
echo '<img src="<?= base_url();?>/assets/images/warning.png">';
}
if($max->format('Y-m-d H:i:s') > $transport->format('Y-m-d H:i:s')) {
echo '<img src="<?= base_url();?>/assets/images/forbidden.png">';
} ?>
<?php
$transport = strtotime($row->transportDate);
$max = strtotime(max($forecast_array));
$interval = abs($max - $transport);
if($max < $transport && $interval >= 2 * 60 * 60 ) {
echo '<img src="<?= base_url();?>/assets/images/tick.png">';
}
Upvotes: 0
Views: 2386
Reputation: 406
$transport= new DateTime($row->transportDate);
$max=new DateTime(max($forecast_array));
$interval = $transport->diff($max);
if ($interval->h > 2) {
//
}
Upvotes: 1
Reputation: 475
To compare date/time differences as simple as your example, I'd suggest to not use the DateTime() class, but use simple timestamps. E.g.:
$transport = strtotime($row->transportDate); // strtotime parses the time if it is not a timestamp, if it already is just use as is, i.e. without strtotime()
$max = strtotime(max($forecast_array));
$intervall = abs($max - $transport);
// $intervall is now the difference in seconds therefore you can do this simple check:
if($interval >= 2 * 60 * 60){ // 2 hours à 60 minutes à 60 seconds
// interval > 2 hours
} else {
//...
}
Upvotes: 4