Validate if age is over 18 years old

前端 未结 6 2338
情歌与酒
情歌与酒 2020-12-14 12:23

Just wondering, can I do this to validate that a user has entered a date over 18?

//Validate for users over 18 only
function time($then, $min)
{
    $then =          


        
6条回答
  •  眼角桃花
    2020-12-14 13:00

    Here is a simplified extract from what I used for a banking system in Toronto, and this always worked perfectly, taking account of leap years of 366 days.

    /* $dob is date of birth in format 1980-02-21 or 21 Feb 1980
     * time() is current server unixtime
     * We convert $dob into unixtime, add 18 years, and check it against server's
     * current time to validate age of under 18
     */
    
    if (time() < strtotime('+18 years', strtotime($dob))) {
       echo 'Client is under 18 years of age.';
       exit;
    }
    

提交回复
热议问题