doulianqi3870 2017-04-11 08:18
浏览 53

生日计算器

<!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.

  • 写回答

2条回答 默认 最新

  • dswqw66280 2017-04-11 08:22
    关注

    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

    评论

报告相同问题?