By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,991 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,991 IT Pros & Developers. It's quick & easy.

User input his/her date of birth and php script calculate how old person is??

P: 1
Hello !
Im having problem with this. I already created index.html document which allows user to input his/her date of birth. It looks like this :
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <center>
  4. <h3>How old are you ??</h3>
  5. <table border="0" width="25%">
  6. <form action="calculate.php" method="get">
  7. <td bgcolor="#FFCC00 ">Your name</td>
  8. <td><input type="text" name="name" maxlength="20" value="name"></td>
  9. </tr>
  10. <td bgcolor="#FFCC00 ">Your last name</td>
  11. <td><input type="text" name="last" maxlength="30" value="lastname"></td>
  12. </tr>
  13. <td bgcolor="#FFCC00">Input day (1-31)</td>
  14. <td><input type="text" name="day" maxlength="2" value="day"></td>
  15. </tr>
  16. <td bgcolor="#FFCC00">Input month (1-12)</td>
  17. <td><input type="text" name="month" maxlength="2" value="month"></td>
  18. </tr>
  19. <td bgcolor="#FFCC00">Input year (exmp. 1970.)</td>
  20. <td><input type="text" name="year" maxlength="4" value="year"></td>
  21. </tr>
  22. <br>
  23. <td colspan="2"><br><center><input type="submit" value="CALCULATE"</center></td>
  24. </form>
  25. </center>
  26. <body>
  27. </html>
....and calculate.php which should calculate how person is old look like this:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head >
  3. <title>YOUR AGE </title>
  4. </head>
  5. <body>
  6. <a href="index.html">BACK</a>
  7. <br><br>
  8. <center>
  9.  
  10. <h3>Current date:
  11. <?php
  12. echo date("d/m/Y");
  13. ?>. </h3>
  14.  
  15. Person
  16. <?php
  17.  echo $_GET["name"];
  18. ?> <?php
  19. echo $_GET["lastname"];
  20. ?> have/has 
  21. <?php
  22.  
  23. if($yr=$_GET["year"])
  24. {
  25. echo $calculate=date("Y")-$yr ; 
  26. }
  27. ?>
  28.  years,
  29. <?php
  30. if($mo=$_GET["month"])
  31. {
  32. echo $calculate2=date("m")-$mo ; 
  33. ?> months and
  34. <?php
  35. if($dn=$_GET["day"])
  36. {
  37. echo $calculate3=date("d")-$dn ; 
  38. ?> days.
  39. </center>
  40. </body>
  41. </html>
When I input birth year for example 17.6.1988. i get this result :
Person John have/has 22 years, -1 months and 3 days.
which is obviously wrong. Does anyone know how to solve this problem ??
This is my college homework and I need answer fast , so if anyone could be kind and solve this I would appreciate it a lot.
Thanks in advance and sorry for my bad english . :)
May 20 '10 #1

✓ answered by code green

I would try a slightly different route:
Convert the dates into unix timestamps - strtotime() will do this.
Do all the calculations in seconds.

Then convert back into human friendly time - something like
divide by 31536000 = number of seconds in a year.

Admittedly gets trickier if you need months because of varying length.
i think php has a function to do this, or something similar

Share this Question
Share on Google+
1 Reply


code green
Expert 100+
P: 1,726
I would try a slightly different route:
Convert the dates into unix timestamps - strtotime() will do this.
Do all the calculations in seconds.

Then convert back into human friendly time - something like
divide by 31536000 = number of seconds in a year.

Admittedly gets trickier if you need months because of varying length.
i think php has a function to do this, or something similar
May 20 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.