简体   繁体   中英

PHP birth checker error

I currently have a registration form which checks a persons date of birth via three inputs

day(dd) - month(mm) - year(yyyy)

The code I am using to check :

    function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);
return $end_date - $start_date;
}
//Enter date of birth below in MM/DD/YYYY
$dob="$day/$month/$year";
$dob2 = "$dob";
$one =  round(dateDiff("/", date("d/m/Y", time()), $dob2)/365, 0) . " years.";
if($one <13){
?>
You must be 13 years of age or older to join!
<?
}else{
?>
YAY you're 13 or above!
<? } ?>

I am receiving an error saying:

error: Warning: gregoriantojd() expects paramater 1 to be long string

Can anyone help me with this?

Thank you in advance!

Why complicate when you can do it in really simple way:

function dateDiff($dateFormat, $beginDate, $endDate)
{
    $begin = DateTime::createFromFormat($dateFormat, $beginDate);
    $end = DateTime::createFromFormat($dateFormat, $endDate);
    $interval = $begin->diff($end);

    if($interval->y >= 13) 
    {
        echo 'Over 13';
    }
    else
    {
        echo 'Not 13';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM