生日计算器

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title> Birthday </title>

<?php

    if(isset($_POST["birthday"]))
        $birthday = $_POST["birthday"];
    else
        $birthday = 1;

    function calculate_age($birthday){

        list($day, $month , $year) = explode("/", $birthday);
        $day_diff = date("d") - $day;
        $month_diff = date("m") - $month;
        $year_diff = date("Y") - $year;
        if($month_diff < 0){
            $year_diff--;
        }
        else if(($month_diff == 0) && ($day_diff < 0)){
             $year_diff--;
        }
        else if (($month_diff == 0) && ($day_diff == 0)){
             echo "<script type='text/javascript'>alert('Happy 
             Birthday!!!');</script>";
        }

         return $year_diff;
        }

        $finalBirthday=calculate_age($birthday);

 ?>

</head>
<body>

     <h1>Birthday Calculator</h1>

     <form name="Birthday" method="POST" action="birthday.php">
        <label>What is your Birthday?</br></label>
        <input type ="text" name = "birthday" VALUE = "DD/MM/YYYY"></br>
        <input type ="submit" name = "submit" VALUE = "Submit"></br>
     </form>

<?php

    echo "Our Birthday Calculator says you are " .$finalBirthday;

 ?> 

 </body>
 </html>

I was wondering why first running this it comes up with "Our Birthday Calculator says you are 2017".

After putting an actual date in, however, the php works properly.

Any help would be greatly appreciated.

well since birthday = 1 the day = 1 and month = null and year = null.

This would be somewhat equivalent to your birthday being 1/0/00 making you 2017 years old

Because the first time the code is run, $birthday is set to 1 and then passed through the calculate_age() function.

the function expects a string representation of a date separated by forward slashes /.

You aren't passing the function what it expects so it's returning gibberish.

Expanded solution: Don't do the calculation unless you have a date:

$finalBirthday = false;

if(isset($_POST["birthday"]))
    $birthday = $_POST["birthday"];

    list($day, $month , $year) = explode("/", $birthday);
    $day_diff = date("d") - $day;
    $month_diff = date("m") - $month;
    $year_diff = date("Y") - $year;
    if($month_diff < 0){
        $year_diff--;
    }
    else if(($month_diff == 0) && ($day_diff < 0)){
         $year_diff--;
    }
    else if (($month_diff == 0) && ($day_diff == 0)){
         echo "<script type='text/javascript'>alert('Happy 
         Birthday!!!');</script>";
    }

    $finalBirthday=$year_diff;

}

...

<?php

    if ($finalBirthday) {
        echo "Our Birthday Calculator says you are " .$finalBirthday;
    }

 ?>