在许多不同的日期范围内分离结果

时间:2014-11-07 10:33:54

标签: php mysql

我需要有一个表格,将日期分成:

1. If the date is within the next 30 days - Red
2. If the date is over 30 days but under 60 - Orange
3. If the date is over 60 days - Green

我似乎无法做到这一点。这是我的select语句,它返回结果:

$result = mysqli_query($con,"SELECT a.ID, a.TITLE, b.VALUE_ID, 
b.UF_CRM_1410772281, b.UF_CRM_1410772330 FROM b_crm_company a 
INNER JOIN b_uts_crm_company b ON a.ID = b.VALUE_ID 
WHERE UF_CRM_1410772281 = 1 GROUP BY a.TITLE ORDER BY UF_CRM_1410772330 ASC");

这是在表格中显示结果的while循环。你会看到我试图在上面指定的结果之间分隔日期(请注意表格在select语句之前开始):

<table width="475">
<tr><th width="275"><b>Company</b></th><th width="200"><b>Renewal Date</b></th></tr>


while($row = mysqli_fetch_array($result))
{
$companyName = $row['TITLE'];
$supportRenewal = date('d/m/Y',strtotime($row['UF_CRM_1410772330']));
$id = $row['ID'];


if(strtotime($row['UF_CRM_1410772330']) < strtotime('+30 day')) {

print "<tr><td><strong style='color:#ff0000;'>$companyName</strong></td><td>
<strong style='color:#ff0000;'>$supportRenewal</span></td></tr>";

}

if ((strtotime($rows['UF_CRM_1410772330']) > strtotime('+30 day')) 
and (strtotime($rows['UF_CRM_1410772330']) < strtotime('+60 day'))) {

print "<tr><td><strong style='color:#ff8400;'>$companyName</strong></td>
<td><strong style='color:#ff8400;'>$supportRenewal</span></td></tr>";

}

if(strtotime($row['UF_CRM_1410772330']) > strtotime('+90 day')) {

print "<tr><td><strong style='color:#ff0000;'>$companyName</strong></td><td>
<strong style='color:#ff0000;'>$supportRenewal</span></td></tr>";

}

}

?>

</table>

当我认为这样做时似乎没有用,有人可以指出我出错的地方

2 个答案:

答案 0 :(得分:1)

你应该这样做

<?php 
$array = array('2014-11-15','2014-12-08','2015-01-10','2015-7-05');
foreach($array as $date) {
    $now = strtotime(date('Y-m-d'));
    $newDate = strtotime($date);
    if($newDate > $now) {
        $dateDiff = ($newDate - $now)/(24*60*60);
        echo $dateDiff; 
        if($dateDiff > 1 && $dateDiff <= 30) {
            echo "Red =======<br>";
        } else if($dateDiff > 30 && $dateDiff < 60) {
            echo "Orange =======<br>";
        } else {
            echo "Green =======<br>";
        }
    }
} 
?>

在您的代码中实现此功能。

<table width="475">
<tr><th width="275"><b>Company</b></th><th width="200"><b>Renewal Date</b></th></tr>


while($row = mysqli_fetch_array($result))
{
$companyName = $row['TITLE'];
$id = $row['ID'];
$now = strtotime(date('Y-m-d'));
$supportRenewal = strtotime($row['UF_CRM_1410772330']);
$dateDiff = ($supportRenewal - $now)/(24*60*60);
if($dateDiff <= 30) {

print "<tr><td><strong style='color:#ff0000;'>$companyName</strong></td><td>
<strong style='color:#ff0000;'>date('Y-m-d',$supportRenewal)</span></td></tr>";

}

if($dateDiff > 30 && $dateDiff < 60) {

print "<tr><td><strong style='color:#ff8400;'>$companyName</strong></td>
<td><strong style='color:#ff8400;'>date('Y-m-d',$supportRenewal)</span></td></tr>";

}

if($dateDiff > 60) {

print "<tr><td><strong style='color:#ff0000;'>$companyName</strong></td><td>
<strong style='color:#ff0000;'>date('Y-m-d',$supportRenewal)</span></td></tr>";

}

}

?>

</table>

答案 1 :(得分:0)

我会尝试使用PHP的DateTime类。     $ now = new DateTime();

$future = new DateTime($row['UF_CRM_1410772330']);

$interval = $now->diff($future);
echo $interval->format('%a');

// Now you can simply check the differences by using if statements