php大于比较不起作用
问题描述:
我正在寻找突出显示12岁及以上孩子时的行,但所有行都突出显示。php大于比较不起作用
简而言之,这里是我的数据。
我得到的出生日期在这里:
// 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”语句中的“字符串”和“整数”有关 - 但不确定接下来要做什么。
任何想法?
答
感谢幸运Chingi方向,我只是说这对我的IF语句,一切都很好与世界:
($ageOnDec2020->format('%y') > 12)
检查ageOnDec2020变量的IF环 –
前值尝试此链接的http:/ /*.com/questions/28621270/php-object-of-class-dateinterval-could-not-be-converted-to-string –
谢谢。我刚刚添加了这个,它的工作原理:($ ageOnDec2020-> format('%y')> 12) – kentrenholm