2014-01-24 85 views
-2

我有age = 39month = 10day = 5,我需要得到出生年份如何在php中获得年龄,月份和日期?

年需要采取在考虑agemonthday

什么想法?

+0

年龄是几年?你可以从当前的日期(“Y”)中减去那个年份吗?/ –

+0

年龄就像'39' – Patrioticcow

+1

使用GOOGLE,搜索关于'date()'和'strtotime()' –

回答

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; 
?> 

这是一个有点笨拙,但它会工作。它会检查他们的生日是否已经发生并计算出来。

+0

这似乎工作。生病选择这个答案,因为我实际上可以用不同的数字进行测试。谢谢大家 – Patrioticcow

0

我刚刚创建了一个小例子,我的出生日期和它的作品,希望这是你在找什么

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

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

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

你知道'strtotime'会考虑今天吗?月和日? – Patrioticcow

+0

这个想法是从当前日期开始计算年份,然后在该年份中添加月份,日期。 –

0

扩展在修正我的意见有缺陷的逻辑......

if($month >= date("m") && $day >= date("d") 
    $year = date("Y") - $age; 
else 
    $year = $date("Y") - $age - 1; 
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); 
相关问题