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 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入