我的任务是能够以某人的生日作为论据,并以这种特定方式返回年龄以及是否生日
getBirthDate('09/06/1992'); // returns array('age' => 21, 'birthday' => true);
我无法让布尔值正常工作,因为它总是正确返回年龄,但无论日期是否正确,总是将“生日”返回为 false。
<?php
function getBirthDate($birthdate) {
$today =getdate();
$day =array($today["mon"], $today["mday"]);
$newBday = str_replace("/",",",$birthdate);
$birthDate = explode(",",$newBday);
$monthDay = array($birthDate["0"],$birthDate[1]);
$compared = array_diff($day,$monthDay);
if ($compared === "") {
$age = ($today["year"]-$birthDate["2"]);
$dateArray = array( "age" => "$age", "birthday" => "True");
return print_r ($dateArray);
}
else {
if ($monthDay[0] < $day[0]) {
$age = (($today["year"]-$birthDate["2"])-1);
$dateArray = array( "age" => "$age", "birthday" => "Fals");
return print_r($dateArray);
}
else {
$age = (($today["year"]-$birthDate["2"]));
$dateArray = array( "age" => "$age", "birthday" => "False");
return print_r($dateArray);
}
}
}
?>