检查日期是否在多个日期范围的范围内

时间:2013-05-14 15:26:30

标签: php codeigniter date date-range

我有一份有效期的证书清单。我试图显示不同的颜色,取决于它们到期有多远。我需要显示红色,如果它在不到30天(或已经过期)到期,琥珀如果日期在60到30天之间,绿色如果超过60天。到目前为止,我只开始工作+30天。到目前为止,我有以下

<?php foreach($company_certificates as $certificates): 
$startdate = date('d-m-Y');
$onemonth = date('d-m-Y', strtotime('+30 days')); 
?>

<tr>
    <td><?php echo $certificates->username ?></td>
    <td><?php echo $certificates->certificate ?></td>
    <td><?php echo $certificates->expiry_date ?></td>
    <td><?php 
    //$onemonth = date($certificates->expiry_date, strtotime('-30 days'));
    if ($date >= $certificates->expiry_date)
    {
        $expiry = '2'; //expired
    }
    else
    {
        if($certificates->expiry_date > $onemonth && $startdate < $certificates->expiry_date) 
        {
            $expiry = '1';
            //echo"1 month";
        }
        else
        {
            $expiry = '0'; 
        }
    }
    echo '<img src="' . $this->config->base_url('assets/images/expiry_' . $expiry . '.png') . ' "/>';?></td></tr>
<?php endforeach; ?>

到目前为止,它显示证书是否已过期,但未显示其他证书的正确到期日期。任何人都可以指出我正确的方向,因为这个问题正在扼杀我。谢谢!

2 个答案:

答案 0 :(得分:1)

看一下DateTime::diff来获取你的间隔,然后格式化很简单

答案 1 :(得分:1)

function getExpireColor($expireDate){
    $today = strtotime(date("Y-m-d"));
    $exp = strtotime($expireDate);
    // convert the difference to days
    $days = ($exp - $today)/(3600*24);
    if ($days < 30) return "red";
    if ($days < 60) return "amber";
    return "green";
}

只调用

$this->config->base_url('assets/images/expiry_' . getExpireColor($expiry) . '.png')