Skip to content
Advertisement

PHP date of birth checker

I am currently using the following PHP code to check date of birth, the code uses the american mm/dd/yyyy although I’m trying to change it to the british dd/mm/yyyy.

I was hoping someone could tell me what to change:

function dateDiff($dformat, $endDate, $beginDate)
{
    $date_parts1=explode($dformat, $beginDate);
    $date_parts2=explode($dformat, $endDate);
    $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
    $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
    return $end_date - $start_date;
}

//Enter date of birth below in MM/DD/YYYY
$dob="04/15/1993";
echo round(dateDiff("/", date("m/d/Y", time()), $dob)/365, 0) . " years.";

Advertisement

Answer

the explode is breaking up the string into an array, it is using the / as the separator.

So for us dates you would get

$date_parts1[0] = 04
$date_parts1[1] = 15
$date_parts1[2] = 1993

what you want is to swap the values at index 0 and 1.

try this:

$start_date=gregoriantojd($date_parts1[1], $date_parts1[0], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[0], $date_parts2[2]);

Edit, added correction from comment: also change the last line to

echo round(dateDiff("/", date("d/m/Y", time()), $dob)/365, 0) . " years.";
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement