如果减法的结果小于2小时,我必须进行比较。我不知道这个比较到底是怎么回事。我试过以下几种方法。对于第三个假设是正确的,但是对于第一个和第二个结果是不正确的。
以下是我的看法:
<?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">';
}
发布于 2015-06-30 16:37:39
为了像您的示例一样简单地比较日期/时间差异,我建议不要使用DateTime()类,而是使用简单的时间戳。例如:
$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 {
//...
}发布于 2015-06-30 16:31:40
$transport= new DateTime($row->transportDate);
$max=new DateTime(max($forecast_array));
$interval = $transport->diff($max);
if ($interval->h > 2) {
//
}https://stackoverflow.com/questions/31143361
复制相似问题