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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP