dongmin3754 2015-11-01 14:15
浏览 52
已采纳

PHP大于比较不起作用

I'm looking to highlight the rows when a child is 12 years and older but all the rows are highlighting.

In a nutshell, here is my data.

I'm getting the birth date here:

      // getting birth date to a string
        $birthDateIn=strtotime($birthDate);
        $newDate=date('Y-m-d',$birthDateIn);

I'm getting the age of the child as of today here:

        // getting the date difference between birthdate and today
        $birthDateIn = new DateTime(''.$newDate.'');
        $todayDateIn = new DateTime('today');
        $age = $birthDateIn->diff($todayDateIn);

I'm looping through the MySQL query results, and then displaying the age in this format:

        echo "<td>" . $age->format('%y years %m months') ."</td>";

Then, this code sets age of child in 2020 (this works):

        //Get age Dec 2020  
        $setDec2020 = new DateTime('2020-12-31');           
        $ageOnDec2020=$birthDateIn->diff($setDec2020);

Now, here is my problem. I'm using this code to display a GREEN span if the age of the child will be over 12 years of age, otherwise, yellow:

        if ($ageOnDec2020 > 12)
        {   
            $ageOnDec2020Display = "<span style='background-color: #98FB98'>".$ageOnDec2020->format('%y') ."</span>";
        } else {

            $ageOnDec2020Display= "<span style='background-color: #FFFF00'>".$ageOnDec2020->format('%y') ."</span>";
        }

Then, I display it as such:

            echo "<td>" . $ageOnDec2020Display ."</td>";

The output is that everything is highlighting as yellow when not all of the values are over 12. I think this has to do with "strings" and "integers" in my "if" statement -- but not sure what to do next.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongsu0308 2015-11-01 14:26
    关注

    Thanks to Lucky Chingi direction, I just added this to my IF statement, and all is good with the world:

      ($ageOnDec2020->format('%y') > 12)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。