php如何比较一天到近端比

时间:2013-10-01 02:48:05

标签: php mysql sql date datetime

我在数据库中有2个字段来关联我的工作(start_daterepeat_nb)。用我的旧问题about how to alert color by date 我完成了代码                 `

     date_default_timezone_set('Asia/Phnom_Penh');
     $today = date('Y-m-d');
     /**** This is just simple to show in browser to see data but not what to need********/
     $current = strtotime($today);
     echo "<br /><br />";
     echo 'CURRENT_DATE: '. ($today) ."\n";
     echo "<br /><br />";
     $start =1379437200;
     echo 'Start date: '. date('Y-m-d', $start) ."\n";
     echo '<br/>';
     echo '<br/>';
     /****it is just for get values of date that pass by values $date******/
     $date = '2013-09-18';
     echo 'Convert: '.$Convert = strtotime($date);
     echo "<br />";
     echo 'DisConvert: '.date('Y-m-d', $Convert);
     /**** start code for procees*****/
     echo "<br /><br /><br /><br /><br />";
     $answer = ($current)-($start);
     echo "<br />";
     if(((($answer/24)/60)/60)==30){
         echo "red";
         exit();
     }
     elseif(((($answer/24)/60)/60) >=24 && ((($answer/24)/60)/60)<29 ){
         echo "yellow";
         exit();
     }
     elseif(((($answer/24)/60)/60) >= 20 && ((($answer/24)/60)/60)<29){
         echo "green";
         exit();
     }
     else{
          echo "nothing";
     }` 

是以上代码由start_date= 1379437200 = 2013-09-18repeat_nb = 30 day声明

我的问题:颜色已经按照我需要的日期发出警告但是我希望在屏幕上有任何颜色的日期显示时将其缩短为绿色和绿色组合,并且这些颜色的末端附近的数据显示在上面(EX: green color have 7 row but some row is just start and some row is near change to yellow)。

注意:此代码的repeat_nb仅为 30天,但实际上可由用户设置为 10天,30天,3个月,6个月,...

请介绍一下它。我希望很快能看到你的回复。

1 个答案:

答案 0 :(得分:0)

以下是基于间隔返回颜色的函数:

使用示例:

echo getColor('2013-09-30'); # green
echo getColor('2013-09-20'); # yellow
echo getColor('2013-09-01'); # red
echo getColor('2013-08-15'); # nothing

代码:

function getColor($date) {
    $now = new DateTime('today');
    $date = new DateTime($date);
    $colors = array(
        '-7 day'   => 'green',
        '-20 day'  => 'yellow',
        '-1 month' => 'red',
    );
    foreach ($colors as $modify => $color) {
        $copy = clone $now;
        $copy->modify($modify);
        if ($copy <= $date) return $color;
    }
    return 'nothing';
}