PHP:当一个人达到特定年龄时,如何获得日期?
我正在进行一项涉及日期的任务。我有一本书。现在我想知道这个人在几个月内达到特定年龄的日期 例如:PHP:当一个人达到特定年龄时,如何获得日期?,php,date,Php,Date,我正在进行一项涉及日期的任务。我有一本书。现在我想知道这个人在几个月内达到特定年龄的日期 例如: 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 ) {
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
}
查看以了解如何编写间隔。同样,PHP5.3.0+是必需的。使用PHP函数,您可以获得您要查找的日期。e、 g
strtotime('+50 months', mktime());
由于您从出生日期计算当前年龄,因此我建议您也使用出生日期而不是当前年龄来计算用户300个月大的时间。以下为上述DateTime解决方案的等效值(不需要5.3): 第二个参数是生日时间戳,上面给出
Tue, 13 Oct 2015 00:00:00 +0200
进一步阅读:
echo date('r', strtotime('+300 months', strtotime('1990-10-13')));
Tue, 13 Oct 2015 00:00:00 +0200
function getReqDate( $startDate, $currAgeMonths, $currAgeDays, $reqAgeMonths ) {
$unixStartDate = strtotime($startDate);
$dateBirth = strtotime("-$currAgeDays months", strtotime("-$currAgeMonths months", $unixStartDate));
return strtotime("+$reqAgeMonths months", $dateBirth);
}
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
}