代码之家  ›  专栏  ›  技术社区  ›  Hilde Schneider

php对我的if条件有问题

  •  0
  • Hilde Schneider  · 技术社区  · 8 年前

    我的if状况似乎有问题。 每当我输入一个过期超过31天的许可证时,它就会进入一个条件,当许可证过期日期大于31天时,它会给出一个绿色圆圈和绿色文本。 它将其视为已过期,并将在31天内到期,并给出红色和绿色圆圈和文本。

    这里有一个片段。

    $exp = strtotime($exp_date);
    
    $td = strtotime($today_date);
    
    $diff= $td -$exp;
    
    $x = abs(floor($diff/ (60 * 60 * 24)));
    
    
    $alertdays = ""; //expired
    
    $alertdays1 = ""; //less than 30 days before expiry
    
    $alertdays2 = ""; //more than 30 days
    
    
    
    
    $statusexp = ""; //expired
    
    $status = ""; //less than 30 days
    $status1 = ""; //more than 30 days
    
    $daysexp = ""; //days expired
    
    
    if($td > $exp)
    {
    $statusexp = '<i class="fa fa-circle" style="font-size:30px; color:red;"></i>';
    }
    
    
    if($diff < 0){
    $status = '<i class="fa fa-circle" style="font-size:30px; color:orange;"></i>';
    }
    
    if($diff <= 30) {
    
    $alertdays1 = $x. ""; 
    
    }
    if($x >= 31){
    $alertdays1 = "";
    $alertdays2 = $x. "";
    $status ='<i class="fa fa-circle" style="font-size:30px; color:green;"></i>'; 
    }
    
    
    if($td > $exp){
    $alertdays ="Expired";
    $daysexp  = " - " . $x; 
    }
    

    Output

    2 回复  |  直到 8 年前
        1
  •  0
  •   Phil    8 年前

    代码的问题在于 ABS 函数计算今天与目标日期的差值。如果您使用 2018-02-01 为了测试它,结果应该是-41,但是使用ABS你得到了41,所以你的代码给出了“绿色”的结果。

        2
  •  0
  •   Mehrdad Dashti    8 年前

    您可以创建函数

    例如:

    <?php
    function Mehrdad_Diff_Alldate($startTime) {
    $start_date = new DateTime($startTime);
    $endTime = date("Y-m-d H:i:s");
    $since_start = $start_date->diff(new DateTime($endTime));
    $text=$since_start->y.'|'.$since_start->m.'|'.$since_start->days.'|'.$since_start->h.'|'.$since_start->i.'|'.$since_start->s.'|';
    return $text;   
    }
    
    $td =  date("Y-m-d H:i:s");
    $exp="2007-08-12 13:10:56";
    $exps = Mehrdad_Diff_Alldate($exp);
    $explode=explode("|",$exps);
    $year=$explode[0];
    $month=$explode[1];
    $day=$explode[2];
    $hours=$explode[3];
    $mintue=$explode[4];
    $second=$explode[5];
    ?>
    

    现在您可以使用from$day 不要把这些术语分开

    例如:

    <?php
    if($td > $exp){
    
    }else if($day < 0){
    
    }else if($day <= 30) {
    
    }else if($day >= 31){
    
    }else if($td > $exp){
    
    }else{
    
    }
    ?>