如何使用PHP& amp;获得从出生日期开始的年龄MySQL的?

时间:2010-04-07 14:44:09

标签: php mysql

我问我的用户他们的生日,并按照以下方式将其存储在我的数据库中$month $day $year输出May 6 1901但我想知道如何才能获得使用PHP& amp;存储的生日的年龄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>

6 个答案:

答案 0 :(得分:3)

如果您只需要计算给定日期的年龄,那么已经发布了许多答案。见这一个:Calculate years from date

答案 1 :(得分:0)

使用datetime:createfromformat,您可以将日期解析为DateTime对象,并对对象进行计算,这使得它非常容易。但需要php 5.3。

答案 2 :(得分:0)

function getAge($then) {
    $then = date('Ymd', strtotime($then));
    $diff = date('Ymd') - $then;
    return substr($diff, 0, -4);
}

根据JYelton的回答,只需将此函数放入您的代码中,然后将日期发送给它:

$age = getAge('June 30 1959');

echo $age;

答案 3 :(得分:0)

试试这个:

$age = floor((time() - mktime(0,0,0,$month,$day,$year))/31536000);

现在花费时间,减去他们出生的时间,将其转换为年份,并将其给予当前年龄。

答案 4 :(得分:0)

你可以尝试这个::

if( isset($_POST['submit']) )   
{
   $day = $_POST['day'];
   $month = $_POST['month'];
   $year = $_POST['year'];

   $cal_date = mktime(0, 0, 0, $month, $day, $year );  // convert in second
   $start_date = date('Y-m-d',$cal_date);   // make real time as  Year-month-day for store value in database

   $birthDay = strtotime("now") - strtotime($start_date);

   echo substr($birthDay, 0, -4);


  }

答案 5 :(得分:0)

在php中创建函数:

function calculateage($BirthDate)
{
list($Day, $Month, $Year) = explode(".", $BirthDate);

$YearDiff = date("Y") - $Year;

if(date("m") < $Month || (date("m") == $Month && date("d") < $DayDiff))
{
$YearDiff--;
}
return $YearDiff;
}

呼叫功能,其中年龄显示为

calculateage($date)

$date在mysql表中是生日的