Advertisement
pusatdata

php calcuator age umur

Jan 22nd, 2024 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. INI YANG BAGUS:
  2.  
  3. <?php
  4. $dob = new DateTime('1978-09-29');
  5. $today = new DateTime;
  6. $age = $today->diff($dob);
  7. echo $age->format('%y year, %m months dan %d days');
  8. ?>
  9.  
  10. <?php $dob = new DateTime('1978-09-29');
  11. $today = new DateTime('today');
  12. $year = $dob->diff($today)->y;
  13. $month = $dob->diff($today)->m;
  14. $day = $dob->diff($today)->d;
  15. echo "Age is"." ".$year. " year"." ",$month. " months"." ".$day. " days";
  16. ?>
  17. =============================
  18.  
  19.  
  20. 1. MODEL PERTAMA
  21. <?php
  22. /**
  23. * Simple PHP age Calculator
  24. *
  25. * Calculate and returns age based on the date provided by the user.
  26. * @param date of birth('Format:yyyy-mm-dd').
  27. * @return age based on date of birth
  28. */
  29. function ageCalculator($dob){
  30. if(!empty($dob)){
  31. $birthdate = new DateTime($dob);
  32. $today = new DateTime('today');
  33. $age = $birthdate->diff($today)->y;
  34. return $age;
  35. }else{
  36. return 0;
  37. }
  38. }
  39. $dob = '1992-03-18';
  40. echo ageCalculator($dob);
  41. ?>
  42.  
  43. ========================
  44.  
  45. 2. MODEL KEDUA
  46. <?php
  47. //date in mm/dd/yyyy format; or it can be in other formats as well
  48. $birthDate = "12/17/1983";
  49. //explode the date to get month, day and year
  50. $birthDate = explode("/", $birthDate);
  51. //get age from date or birthdate
  52. $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
  53. ? ((date("Y") - $birthDate[2]) - 1)
  54. : (date("Y") - $birthDate[2]));
  55. echo "Age is:" . $age;
  56. ?>
  57.  
  58. ========================
  59.  
  60. 3. MODEL KETIGA >> SINGKT
  61.  
  62. <?php
  63. $dob='1981-10-07';
  64. $diff = (date('Y') - date('Y',strtotime($dob)));
  65. echo $diff;
  66. ?>
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement