php - 如何使用 PHP 和 MySQL 从出生日期获取年龄?

标签 php mysql

我询问用户的出生日期,并通过以下方式将其存储在我的数据库中 $month $day $year 输出 1901 年 5 月 6 日 但我想知道如何使用 PHP 和 MySQL 从存储的生日中获取年龄?

这是 PHP 代码。

if (isset($_POST['submitted'])) {

$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*
                             FROM users 
                             WHERE user_id=3");

$month_options = array("Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

$day_options = array("Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31");

$month = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['month'])));
$day = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['day'])));
$year = mysqli_real_escape_string($mysqli, htmlentities(strip_tags($_POST['year'])));


    if (mysqli_num_rows($dbc) == 0) {
            $mysqli = mysqli_connect("localhost", "root", "", "sitename");
            $dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, month, day, year) 
                                         VALUES ('$user_id', '$month', '$day', '$year')");
    }


    if ($dbc == TRUE) {
            $dbc = mysqli_query($mysqli,"UPDATE users 
                                         SET month = '$month', day = '$day', year = '$year'
                                         WHERE user_id = '$user_id'");

            echo '<p class="changes-saved">Your changes have been saved!</p>';

    }


    if (!$dbc) {
            print mysqli_error($mysqli);
            return;
    }
}

这是 html。

<form method="post" action="index.php">
    <fieldset>
        <ul>
            <li><label>Date of Birth: </label>
            <label for="month" class="hide">Month: </label>
            <?php // month options

            echo '<select name="month" id="month">' . "\n";
              foreach($month_options as $option) {
                if ($option == $month) {
                  echo '<option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected">' . stripslashes(htmlentities(strip_tags($option))) . '</option>' . "\n";
                } else {
                  echo '<option value="'. stripslashes(htmlentities(strip_tags($option))) . '">' . stripslashes(htmlentities(strip_tags($option))) . '</option>'."\n";
                }
              }
            echo '</select>';

            ?>
            <label for="day" class="hide">Day: </label>
            <?php // day options

            echo '<select id="day" name="day">' . "\n";
              foreach($day_options as $option) {
                if ($option == $day) {
                  echo '<option value="' . stripslashes(htmlentities(strip_tags($option))) . '" selected="selected">' . stripslashes(htmlentities(strip_tags($option))) . '</option>' . "\n";
                } else {
                  echo '<option value="'. stripslashes(htmlentities(strip_tags($option))) . '">' . stripslashes(htmlentities(strip_tags($option))) . '</option>'."\n";
                }
              }
            echo '</select>';

            ?>              
            <label for="year" class="hide">Year: </label><input type="text" name="year" id="year" size="4" maxlength="4" value="<?php if (isset($_POST['year'])) { echo  stripslashes(htmlentities(strip_tags($_POST['year']))); } else if(!empty($year)) { echo  stripslashes(htmlentities(strip_tags($year))); } ?>" /></li>


            <li><input type="submit" name="submit" value="Save Changes" class="save-button" />
                <input type="hidden" name="submitted" value="true" />
            <input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
            </ul>
    </fieldset>

</form>

最佳答案

如果您只需要计算给定日期的年龄,已经发布了很多对此问题的答案。看这个:Calculate years from date

关于php - 如何使用 PHP 和 MySQL 从出生日期获取年龄?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593388/

相关文章:

php - 实现 LDAP 合规性

sql - 根据某些列值复制行?

mysql - 在 SQL 查询结果中使用 Format DE_DE 和无限小数位

PHP 没有从 MySQL 数据库中获取正确的数据

php - 使用 PHP 将 HTML 表单数据插入 MySQL

PHP 数组 - 从 CSV 文件读取,创建第一列键,第二列值

php - 如何使用php从mysql列中提取超过1mb的数据

php 只检索数字,即使是字符串

php - EXISTS 总是返回真简单查询

php - 找到最近的主要城市