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

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类