2010-07-26 42 views
2

我正在处理涉及日期的任务。我有一个person's age in months+days。现在我想知道这个人在几个月内达到特定年龄的日期。PHP:如何获得一个人达到特定年龄的日期?

例如:

A person is 250 months and 15 days old on 2010-1-25. 
On which date this person will become 300 months old? 

函数签名可能是:

function getReqDate($startDate, $currAgeMonths, $currAgeDays, $reqAgeMonths ) { 
     //return date 
} 
+0

不是一个答案,但有你需要闰年重新做一个思考:你的规格。 – Tobiasopdenbrouw 2010-07-26 09:45:18

+0

并考虑到(2月除外)其他月份有30天和31天。 – ubiquibacon 2010-07-26 10:10:12

回答

7

既然你计算从生日的现在的年龄,我建议你也使用出生日期,而不是目前的年龄,当用户得到300个月老来计算。以下是以上给出的日期时间溶液的当量(不要求5.3):

echo date('r', strtotime('+300 months', strtotime('1990-10-13'))); 

随着第二PARAM作为生日时间戳上述会给

Tue, 13 Oct 2015 00:00:00 +0200 

进一步阅读:

4
$date = new DateTime('1990-10-13'); 
$date->add(new DateInterval('P300M')); 
echo $date->format('r'); 

DateInterval就看你怎么写的时间间隔。同样,需要PHP 5.3.0+。

0

使用php strtotime函数可以得到您要查找的日期。例如

strtotime('+50 months', mktime()); 
0
function getReqDate($startDate, $currAgeMonths, $currAgeDays, $reqAgeMonths ) { 
     $unixStartDate = strtotime($startDate); 
     $dateBirth = strtotime("-$currAgeDays months", strtotime("-$currAgeMonths months", $unixStartDate)); 
     return strtotime("+$reqAgeMonths months", $dateBirth); 
} 
0
function getReqDate($startDate, $reqAgeMonths) { 
    list($year,$month,$day) = explode('-',$startDate); 
    $date = date("Y-m-d", mktime(12, 0, 0, $month+$reqAgeMonths, $day, $year)); 
    return $date 
} 
相关问题