Yaş 18 yaşın üzerinde olup olmadığını doğrulamak

3 Cevap

Sadece merak, bir kullanıcı 18 üzerinde bir tarih girdiğini doğrulamak için bunu yapabilirsiniz?

//Validate for users over 18 only
function time($then, $min)
{
$then = strtotime('March 23, 1988');
//The age to be over, over +18
$min = strtotime('+18 years', $then);
echo $min;
if(time() < $min) 
{
die('Not 18'); 
}
}

Just stumbled across this function date_diff: http://www.php.net/manual/en/function.date-diff.php Looks, even more promising.

3 Cevap

Neden değil? Benim için tek sorun, Kullanıcı Arabirimi - Eğer kullanıcı zarif hata mesajı göndermek nasıl.

(Eğer sabit bir doğum günü kullanıyorsanız) alımı uygun bir doğum vermedi gibi başka not, sizin fonksiyonu düzgün çalışmayabilir. Daha sonra $ için '23 Mart 1988' değiştirmek gerekir

//Validate for users over 18 only
function validateAge($then, $min)
{
    // $then will first be a string-date
    $then = strtotime($then);
    //The age to be over, over +18
    $min = strtotime('+18 years', $then);
    echo $min;
    if(time() < $min)  {
        die('Not 18'); 
    }
}

Yoksa yapabilirsiniz:

// validate birthday
function validateAge($birthday, $age = 18)
{
    // $birthday can be UNIX_TIMESTAMP or just a string-date.
    if(is_string($birthday)) {
        $birthday = strtotime($birthday);
    }

    // check
    // 31536000 is the number of seconds in a 365 days year.
    if(time() - $birthday < $age * 31536000)  {
        return false;
    }

    return true;
}
if( strtotime("1988/03/23") < (time() - (18 * 60 * 60 * 24 * 365))) {
  print "yes";
} else {
  print "no";
}

Ancak ... atılımlar yıldır muhasebe değil

Burada Toronto'da bir bankacılık sistemi için kullanılan ne kadar basitleştirilmiş bir özü olduğunu, ve bu her zaman 366 gün, artık yıllarda dikkate alarak, mükemmel çalıştı.

/* $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;
}