dpgbh20688 2014-02-22 05:15
浏览 33

PHP - 通过手动设置日期来确定和计算年龄

I have php code where it calculate the current age from birth date.

My problem how do I determine the age if I simply predict what will the age of the person at a given Date.

For example what will be the age of the person by the year 2020 May 30 and not the current Year we have which is 2014, added will be how many months it has been since that person's birthday.

I didn't find any related post here since most of it calculate the age at the current date.

Here's the code for calculating age at the current DATE.

 <?php
  // if-> To Check whether form has been submitted
     if(isset($_POST['submit']))
     {
     $d=$_POST['day'];
     $m=$_POST['month'];
     $y=$_POST['year'];

     //converting Inputed DOB in "dd-mm-yyyy" format
     $dob=$d.'-'.$m.'-'.$y;

     //Create a new date object with current system date
     $bday=new DateTime($dob);

     //Differentiating both dates
     $age=$bday->diff(new DateTime);

     $today=date('d-m-Y'); //Current date
     echo '<br /><br /><br />';
     echo '<b>Your Birth date: </b>';
     echo $dob;
     echo '<br><b>Today : </b> ';
     echo $today;
     echo '<br>';
     echo '<b>Your Age : </b> ';
     echo $age->y;
     echo ' Years, ';
     echo $age->m;
     echo ' Months, ';
     echo $age->d;
     echo ' Days';
     }
     ?>

I simply want to determine the age of that person in advance with Years and Months.

Tnx in advance.

  • 写回答

1条回答 默认 最新

  • dongshan1959 2014-02-22 09:43
    关注

    The time() function gets the number of seconds since the last UNIX Epoch(Jan. 1st, 1970). UNIX time stamps can be used to easily get the differences in times by adding or subtracting the number of seconds between each date and then converting them to larger units(e.g. days, months, years). Simply add an extra form input to get the future date(or specify it in your code) and then use something like this...

    <?php
    
    $d=$_POST['day'];
         $m=$_POST['month'];
         $y=$_POST['year'];
    
    $dob=$d.'-'.$m.'-'.$y;
    
    $fd=$_POST['future_day'];
         $fm=$_POST['future_month'];
         $fy=$_POST['future_year'];
    
    $specified_date = $fd.' - '.fm.' - '.fy;
    
    echo calculate_age($dob, $specified_date);
    
    function calculate_age($date_of_birth, $specified_date){
    
    $unix_dob = strtotime($specified_date); //Convert inputted date of birth to unix timestamp
    $unix_today = time(); //Current unix timestamp
    
    $unix_diff = $unix_today - $unix_dob; //Difference between current unix and DOB unix
    
    $current_age = floor($unix_diff / (60 * 60 * 24 * 365)); 
    
    return $current_age;
    
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路