//check date is valid
function checkbirthdate($month,$day,$year)
{
$min_age = 18;
$max_age = 122;
if(!checkdate($month,$day,$year))
{
return false;
}
$now = new DateTime();
$then_formatted = sprintf("%d-%d-%d",$year,$month,$day);
$then = DateTime::createFromFormat("Y-n-j|",$then_formatted);
$age = $now->diff($then);
if(($age->y < $min_age) || ($age->y > $max_age))
{
return false;
}
else
{
return true;
}
}
if(checkbirthdate(10,25,1990))
{
print("You may use this Web Site!
");
}
else
{
print("You are too yong (or too old!!) to proceed.");
}
?>
知識點1:checkdate($month , $day , $year)
如果$month 在1到12之間,$year在1到32767之間,$day在1到$month和$year所對應的最大天數之間,函數checkdate()會返回true.
知識點2:DateTime類的createFromFormat()方發
該方法非常靈活,告訴他一個格式化的日期字符串結構格式,它就能適當地分解出各個部分,計算正確的時間戳。
知識點3:diff是類DateTime的一個方法
計算兩個日期之間的差值