简体   繁体   English

PHP / MYSQL使用datediff显示mysql记录年龄

[英]PHP/MYSQL display mysql record age using datediff

I'm new to PHP/MYSQL, I'm trying to display the age of all records from my database using DATEIFF as shown in the line 17 of my code, but it just won't work. 我是PHP / MYSQL的新手,我正在尝试使用DATEIFF显示数据库中所有记录的使用DATEIFF ,如代码的第17行所示,但这是行不通的。 I need somebody to help me out with this. 我需要有人帮我解决这个问题。

$result = mysqli_query($con,"SELECT * FROM growers");

echo "<table class='table table-striped table-advance table-hover'>
    <tbody>
        <tr>
            <th><i class='icon_profile'></i>&nbsp;Batch</th>
            <th><i class='icon_ol'></i>&nbsp;Date Received</th>
            <th><i class='icon_clock_alt'></i>&nbsp;Age when Received</th>
            <th><i class='icon_clock_alt'></i>&nbsp;Current Age</th>
            <th><i class='icon_star'></i>&nbsp;NO of Birds</th>
            <th><i class='icon_info'></i>&nbsp;View More</th>
        </tr>";

        while($row = mysqli_fetch_array($result))
        {
            echo"<tr>";
            echo"<td>" . $row['BATCH'] . "</td>";
            echo"<td>" . $row['BIRTH DAY'] . "</td>";
            echo"<td>" . $row['AGE'] . "&nbsp;Week(s)" . "</td>";
            echo"<td>" . "SELECT DATEDIFF("NOW()", "$row['BIRTH DAY']") AS CURRENT AGE". "</td>";
            echo"<td>" . $row['NO OF BIRDS'] . "</td>";
            echo"<td>" . $row['AGE'] . "</td>";
            echo"</tr>";
        }
echo "</table>";

mysqli_close($con);
?>

You could select in main select 您可以在主选择中选择

  $result = mysqli_query($con,
         "SELECT 
        growers.BATCH, 
        growers.`BIRTH DAY`, 
        growers.AGE,            
        DATEDIFF(NOW(),growers.`BIRTH DAY`) AS CURRENT_AGE,
        growers.`NO OF BIRDS`            
        FROM growers; "
   );


                        echo "<table class='table table-striped table-advance table-hover'>
                         <tbody>
                      <tr>
                         <th><i class='icon_profile'></i>&nbsp;Batch</th>
                         <th><i class='icon_ol'></i>&nbsp;Date Received</th>
                         <th><i class='icon_clock_alt'></i>&nbsp;Age when Received</th>
                         <th><i class='icon_clock_alt'></i>&nbsp;Current Age</th>
                         <th><i class='icon_star'></i>&nbsp;NO of Birds</th>
                         <th><i class='icon_info'></i>&nbsp;View More</th>
                      </tr>";

                        while($row = mysqli_fetch_array($result))
                        {
                         echo"<tr>";
                         echo"<td>" . $row['BATCH'] . "</td>";
                         echo"<td>" . $row['BIRTH DAY'] . "</td>";
                         echo"<td>" . $row['AGE'] . "&nbsp;Week(s)" . "</td>";
                         echo"<td>" . $row['CURRENT_AGE'] . "</td>";
                         echo"<td>" . $row['NO OF BIRDS'] . "</td>";
                         echo"<td>" . $row['AGE'] . "</td>";
                         echo"</tr>";
                        }
                        echo "</table>";

                        mysqli_close($con);
                        ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM