如何在PHP中获得年龄,月份和日期的年份?

时间:2014-01-24 18:06:23

标签: php date

我有age = 39month = 10day = 5,我需要获得出生年份

这一年需要考虑agemonthday

任何想法?

4 个答案:

答案 0 :(得分:1)

你可以试试这个:

<?php
  $age = 21;
  $birthmonth = 8;
  $birthday = 26;
  $thisyear = date('Y', time());
  $checkdate = "$thisyear-$birthmonth-$birthday";
  if((time()-strtotime($checkdate)) > 0){
       $birthyear = $thisyear - $age;
  } else {
       $birthyear = $thisyear - $age - 1;
  }
  $fullstring = "$birthyear-$birthmonth-$birthday";
  $fullstring = date('m/d/Y', strtotime($fullstring));
  echo $fullstring;
?>

它有点笨重,但它会起作用。它检查他们的生日是否已经发生并计算出来。

答案 1 :(得分:0)

我刚刚用我的DOB创建了一个小例子,它可以正常工作,希望这就是你要找的东西

$age = 34 ;
$mon = 01;
$day = 29 ;

$beck_year = date("Y",strtotime("- $age years"));

$dob = $beck_year.'-'.$mon.'-'.$day ;

答案 2 :(得分:0)

在评论中扩展纠正我错误的逻辑......

if( $month >= date("m") && $day >= date("d")
    $year = date("Y") - $age;
else
    $year = $date("Y") - $age - 1;

答案 3 :(得分:0)

检查一下希望这会有所帮助!

$years = 10;
$months = 1;
$days = 1;

function get_byear($years,$months,$days ){
    $b_year = date('Y', strtotime("$years years $months months $days days ago"));
    return $b_year;
}

echo get_byear($years,$months,$days);