echo calculateBirthDate('april',28); # 4/1/1993 given current month is May
echo calculateBirthDate('may',28); # 4/1/1992 given current month is May
echo calculateBirthDate('mayxyz',28); # NULL given current month is May
echo calculateBirthDate('may',x1x); # NULL given current month is May
function calculateBirthDate($monthName,$age){
//CONVERT BIRTH MONTH NAME TO INTEGER
$d = date_parse($monthName);
$birthMonthNumber = $d['month'];
//IF DATA IS BAD, RETURN NULL
if(!$birthMonthNumber){
return NULL;
}
if(is_numeric($age)){
$age = intval($age);
} else {
return NULL;
}
//DETERMINE BIRTH YEAR
$currentYear = date("Y");
$currentMonth = date("n");
if( $birthMonthNumber >= $currentMonth ){ //IF BIRTH MONTH IS CURRENT OR IN THE FUTURE, THE BIRTH YEAR SHOULD BE 1 YEAR LESS THAN IF THE BIRTH MONTH HAS ALREADY OCCURRED.
$birthYear = ($currentYear - $age) -1;
} else {
$birthYear = $currentYear - $age;
}
//CREATE DATE STRING USING 1 AS THE DAY
$dateString = $birthMonthNumber . '/1/' . $birthYear;
return $dateString;
}
$age = 46;
$birth_month = 5;
if(date('m') < $birth_month){ $age = $age + 1;}
$birth_month = str_pad($birth_month, 2, '0', STR_PAD_LEFT);
echo date("Y-$birth_month-01", strtotime("today -$age years"));
ASKER
if(date('m') <= $birth_month){ $age = $age + 1;}
ASKER
Why make things so complicated.
ASKER
ASKER
ASKER
what is the nature of data getting to your php?
ASKER
Why not do it simpler.
Current year -age
If current month is before birth month, subtract an additional year.
ASKER
ASKER
BirthMonth, Age
April, 28
June, 52
,37
xyz,
December, xx
ASKER
is this a guess what my Date of Birth is?
echo calculateBirthDate('april',28);
and creating a date to represent the estimated birthdate what would end up like 4/1/1993 while at the same time checking to make sure the data is in proper form.ASKER
ASKER
$birthYear = (date('m') <= date('m', strtotime($month)) ? (int)date('Y') - $age - 1 : (int)date('Y') - $age);
PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.
TRUSTED BY
You have age and month"
Why not do it simpler.
Current year -age
If current month is before birth month, subtract an additional year.