如何使用php和Html显示年龄

时间:2017-10-22 07:33:22

标签: php

how can i put the age inside the table please

$dob=new DateTime($row[DOB]);  // Create a date object from the DOB field
          $age=$cd->diff($dob);          // Find the difference between the 2 dates
          echo $age->format(" Age:%y Years %m Months %d Days");  // Display the years, months and days
               echo"<td>".$row[$age]."</td>";
               echo "<td>".$row[WEIGHT]."</td>";
                echo "<td>".$row[UNIT_WEIGHT]."</td>";

1 个答案:

答案 0 :(得分:0)

考虑更好地描述您的问题,但据我所知,这是您可能正在寻找的答案。

在HTML表的行输出之前使用'echo'作为年龄输出。 您应该在所需的单元格中使用format函数调用。 请检查以下代码:

$dob=new DateTime($row[DOB]);  // Create a date object from the DOB field
$age=$cd->diff($dob);          // Find the difference between the 2 dates
echo "<td>".$age->format("%y Years %m Months %d Days")."</td>";
               echo "<td>".$row[WEIGHT]."</td>";
                echo "<td>".$row[UNIT_WEIGHT]."</td>";

另一个注意事项,通常年龄仅以年为基础。 所以而不是:

  

年龄 - 15岁10个月24天

您可能只想显示年份

  

15(年)

在这种情况下只需更新日期对象格式的参数。

$age->format("%y");