php大于比较不起作用

时间:2015-11-01 14:15:32

标签: php string int

我希望在孩子12岁及以上时突出显示行,但所有行都突出显示。

简而言之,这是我的数据。

我在这里得到了出生日期:

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

我今天已经到了这个孩子的年龄:

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

我循环遍历MySQL查询结果,然后以这种格式显示年龄:

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

然后,这段代码设定了2020年的孩子年龄(这是有效的):

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

现在,这是我的问题。如果孩子的年龄超过12岁,我会使用此代码显示绿色范围,否则为黄色:

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

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

然后,我这样显示:

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

输出是当所有值都不超过12时,所有内容都突出显示为黄色。我认为这与我的“if”语句中的“strings”和“integers”有关 - 但不知道该怎么做下一个。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

感谢Lucky Chingi的指导,我刚刚将其添加到我的IF声明中,并且对世界都很好:

  ($ageOnDec2020->format('%y') > 12)
相关问题